1 回答

TA貢獻(xiàn)2016條經(jīng)驗(yàn) 獲得超9個(gè)贊
1. php中增加數(shù)組元素的方法:
(1)通過(guò)賦值增加數(shù)組元素 :$states[‘name’]=’Tom’;
(2)int array_push(array target_array,mixed variable [,mixed variable…]) 函數(shù)將variable增加到target_array的末尾,成功時(shí)返回true,否則返回false,其中variable可以是多個(gè);
(3)int array_unshift(array target_array,mixed variable [,mixed variable…]) 函數(shù)將variable增加到target_array的數(shù)組頭,成功時(shí)返回true,否則返回false,其中variable可以是多個(gè)。所有已有的數(shù)值鍵都會(huì)相應(yīng)地修改,而關(guān)聯(lián)鍵不受影響;
(4)array array_pad(array target_array,integer length,mixed pad_value) 將target_array 的大小增加到length指定的長(zhǎng)度。
具體方法:
1.使用array_merge方法實(shí)現(xiàn)類(lèi)似array_unshift在開(kāi)頭添加元素的功能
代碼如下:
<?php
$queue = array('a', 'B');
$queue = array_merge(array('front' => 'hello'), $queue);
/*
Array
(
[front] => hello
[0] => a
[1] => b
)
*/
?>
2.+操作符
代碼如下:
<?php
$queue = array('a', 'B');
$queue = array('front' => 'Hello') + $queue;
?>
輸出結(jié)果與使用array_merge方法一樣。
3.在元素結(jié)尾添加關(guān)聯(lián)數(shù)組元素
代碼如下:
<?php
$queue = array('a', 'B');
$queue['front'] = 'hello';
/*
輸出
Array
(
[0] => a
[1] => b
[front] => hello
)
*/
?>
- 1 回答
- 0 關(guān)注
- 1485 瀏覽
添加回答
舉報(bào)