您好,登錄后才能下訂單哦!
這篇文章主要講解了“如何操作PHP數(shù)組array類”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“如何操作PHP數(shù)組array類”吧!
array_merge($arr1,$arr2....);//合并一個或多個數(shù)組
例:
1、
<?php $beginning = 'foo'; $end = array(1 => 'bar'); $result = array_merge((array)$beginning, (array)$end); print_r($result); ?>
以上例程會輸出:
Array
(
[0] => foo
[1] => bar
)
2、
<?php $array1 = array("color" => "red", 2, 4); $array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4); $result = array_merge($array1, $array2); print_r($result); ?>
以上例程會輸出:
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
3、
<?php $array1 = array(); $array2 = array(1 => "data"); $result = array_merge($array1, $array2); ?>
別忘了數(shù)字鍵名將會被重新編號!
Array
(
[0] => data
)
4、
<?php $array1 = array(0 => 'zero_a', 2 => 'two_a', 3 => 'three_a'); $array2 = array(1 => 'one_b', 3 => 'three_b', 4 => 'four_b'); $result = $array1 + $array2; var_dump($result); ?>
運行結(jié)果:
array(5) {
[0]=>
string(6) "zero_a"
[2]=>
string(5) "two_a"
[3]=>
string(7) "three_a"
[1]=>
string(5) "one_b"
[4]=>
string(6) "four_b"
}
array_diff($arr1,$arr2);//計算數(shù)組的差集(對比返回在 array1 中但是不在 array2 及任何其它參數(shù)數(shù)組中的值。)
例:
<?php $array1 = array("a" => "green", "red", "blue", "red"); $array2 = array("b" => "green", "yellow", "red"); $result = array_diff($array1, $array2); print_r($result); ?>
在 $array1 中多次出現(xiàn)的值一樣處理,輸出結(jié)果為:
Array
(
[1] => blue
)
array_diff_assoc($arr1,$arr2...);//帶索引檢查計算數(shù)組的差集
array_diff_assoc() 返回一個數(shù)組,該數(shù)組包括了所有在 array1 中但是不在任何其它參數(shù)數(shù)組中的值
<?php $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red"); $array2 = array("a" => "green", "yellow", "red"); $result = array_diff_assoc($array1, $array2); print_r($result); ?>
以上例程會輸出:
Array
(
[b] => brown
[c] => blue
[0] => red
)
array_keys($array);//返回數(shù)組中部分的或所有的鍵名(返回 input 數(shù)組中的數(shù)字或者字符串的鍵名。)
<?php $array = array(0 => 100, "color" => "red"); print_r(array_keys($array)); $array = array("blue", "red", "green", "blue", "blue"); print_r(array_keys($array, "blue")); $array = array("color" => array("blue", "red", "green"), "size" => array("small", "medium", "large")); print_r(array_keys($array)); ?>
以上例程會輸出:
Array
(
[0] => 0
[1] => color
)
Array
(
[0] => 0
[1] => 3
[2] => 4
)
Array
(
[0] => color
[1] => size
)
array_slice($array, $offset, $length);//從數(shù)組中取出一段
參數(shù):array::輸入的數(shù)組, offset:序列偏移量, length:單元長度
$input = array("a", "b", "c", "d", "e"); $output = array_slice($input, 2); // returns "c", "d", and "e" $output = array_slice($input, -2, 1); // returns "d" $output = array_slice($input, 0, 3); // returns "a", "b", and "c"
感謝各位的閱讀,以上就是“如何操作PHP數(shù)組array類”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對如何操作PHP數(shù)組array類這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。