PHP數組的合並、拆分、區別取值函數集
都說PHP的數組功能很強大、只有真正用於項目工作當中才能夠感受得到,至少我認為是,現在已慢慢的發覺其中的奧秘了……
合並數組有三個函數:
1.array_combine()
攜帶兩個參數數組,參數數組一的值作新數組的鍵,參數數組二的值作新數組的值。很簡單。
例子:
<?php
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);
print_r($c);
?>
上例將輸出:
Array
(
[green] => avocado
[red] => apple
[yellow] => banana
)
2.array_merge()
攜帶兩個參數數組,簡單的將數組二追加到數組一的後面構成新數組。
例子:
<?php
$arrayarray1 = array("color" => "red", 2, 4);
$arrayarray2 = 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.array_merge_recursive()
與上面函數雷同,唯一的區別是在追加時發現要添加的鍵已存在時,array_merge()的處理方式是覆蓋前面的鍵值,array_merge_recursive()的處理方式是重構子數組,將重復的鍵的值組成一個新的數值數組。
例子:
<?php
$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);
?>
上例將輸出
$result:
Array
(
[color] => Array
(
[favorite] => Array
(
[0] => red
[1] => green
)
[0] => blue
)
[0] => 5
[1] => 10
)
拆分數組有兩個函數:
1.array_slice()
攜帶三個參數,參數一為目標數組,參數二為offset,參數三為length。作用為,從目標數組中取出從offset開始長度為length的子數組。
如果offset為正數,則開始位置從數組開頭查offset處,如果offset為負數開始位置從距數組末尾查offset處。如果length 為正數,則毫無疑問取出的子數組元素個數為length,如果length為負數,則子數組從offset開始到距數組開頭count(目標數 組)-|length|處結束。特殊地,如果length為空,則結束位置在數組結尾。
例子:
<?php
$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"
// note the differences in the array keys
print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));
?>
上例將輸出:
Array
(
[0] => c
[1] => d
)
Array
(
[2] => c
[3] => d
)
2.array_splice()
攜帶三個參數,同上,作用是刪除從offset開始長度為length的子數組。
例子:
<?php
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
// $input is now array("red", "green")
$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, -1);
// $input is now array("red", "yellow")
$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, count($input), "orange");
// $input is now array("red", "orange")
$input = array("red", "green", "blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));
// $input is now array("red", "green",
// "blue", "black", "maroon")
$input = array("red", "green", "blue", "yellow");
array_splice($input, 3, 0, "purple");
// $input is now array("red", "green",
// "blue", "purple", "yellow");
?>
區別取值函數有四個:
1.array_intersect()
攜帶參數不定,均為數組,返回所有數組中公共元素的值組成的數組,數組的鍵由所在第一個數組的鍵給出。
例子:
<?php
$arrayarray1 = array("a" => "green", "red", "blue");
$arrayarray2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
?>
上例將輸出:
Array
(
[a] => green
[0] => red
)
2.array_intersect_assoc()
在前一個函數的基礎上,返回所有數組中鍵、值均相同的鍵值對。
例子:
<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result_array = array_intersect_assoc($array1, $array2);
?>
上例將輸出:
Array
(
[a] => green
)
3.array_diff()
攜帶多個數組,返回第一個數組中有的而後面的數組中沒有的所有的值組成的新數組,對應鍵取自第一個數組。
例子:
<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result);
?>
上例將輸出:
Array
(
[1] => blue
)
4.array_diff_assoc()
在前一個函數的基礎上,不僅需要匹配值還要匹配鍵。
例子:
<?php
$array1 = array ("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array ("a" => "green", "yellow", "red");
$result = array_diff_assoc($array1, $array2);
?>
上例將輸出:
Array
(
[b] => brown
[c] => blue
[0] => red
)
作者“飛翔的人生”