array_merge()函数将数组合并到一起,返回一个联合的数组。所得到的数组以第一个输入数组参数开始,按后面数组参数出现的顺序依次迫加。其形式为:
array array_merge (array array1 array2…,arrayN)
这个函数将一个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面。返回作为结果的数组。
如果输入的数组中有相同的字符串键名,则该键名后面的值将覆盖前一个值。然而,如果数组包含数字键名,后面的值将不会覆盖原来的值,而是附加到后面。
如果只给了一个数组并且该数组是数字索引的,则键名会以连续方式重新索引。
<?php $fruits = array("apple","banana","pear"); $numbered = array("1","2","3"); $cards = array_merge($fruits, $numbered); print_r($cards); // output // Array ( [0] => apple [1] => banana [2] => pear [3] => 1 [4] => 2 [5] => 3 ) ?>
<?php $fruit1 = array("apple" => "red", "banana" => "yellow"); $fruit2 = array("pear" => "yellow", "apple" => "green"); $result = array_merge_recursive($fruit1, $fruit2); print_r($result); // output // Array ( [apple] => Array ( [0] => red [1] => green ) [banana] => yellow [pear] => yellow ) ?>
<?php $name = array("apple", "banana", "orange"); $color = array("red", "yellow", "orange"); $fruit = array_combine($name, $color); print_r($fruit); // output // Array ( [apple] => red [banana] => yellow [orange] => orange ) ?>
$fruits = array("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon"); $subset = array_slice($fruits, 3); print_r($subset); // output // Array ( [0] => Pear [1] => Grape [2] => Lemon [3] => Watermelon ) $fruits = array("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon"); $subset = array_slice($fruits, 2, -2); print_r($subset); // output // Array ( [0] => Orange [1] => Pear [2] => Grape )
$fruits = array("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon"); $subset = array_splice($fruits, 4); print_r($fruits); print_r($subset); // output // Array ( [0] => Apple [1] => Banana [2] => Orange [3] => Pear ) // Array ( [0] => Grape [1] => Lemon [2] => Watermelon )
$fruits = array("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon"); $subset = array_splice($fruits, 2, -1, array("Green Apple", "Red Apple")); print_r($fruits); print_r($subset); // output // Array ( [0] => Apple [1] => Banana [2] => Green Apple [3] => Red Apple [4] => Watermelon ) // Array ( [0] => Orange [1] => Pear [2] => Grape [3] => Lemon )
<?php $fruit1 = array("Apple","Banana","Orange"); $fruit2 = array("Pear","Apple","Grape"); $fruit3 = array("Watermelon","Orange","Apple"); $intersection = array_intersect($fruit1, $fruit2, $fruit3); print_r($intersection); // output // Array ( [0] => Apple ) ?>
$fruit1 = array("red"=>"Apple","yellow"=>"Banana","orange"=>"Orange"); $fruit2 = array("yellow"=>"Pear","red"=>"Apple","purple"=>"Grape","1"=>"Orange","orange"=>"Orange"); $fruit3 = array("green"=>"Watermelon","orange"=>"Orange","red"=>"Apple"); $intersection = array_intersect_assoc($fruit1, $fruit2, $fruit3); print_r($intersection); // output // Array ( [red] => Apple [orange] => Orange )
$fruit1 = array("Apple","Banana","Orange"); $fruit2 = array("Pear","Apple","Grape"); $fruit3 = array("Watermelon","Orange","Apple"); $intersection = array_diff($fruit1, $fruit2, $fruit3); print_r($intersection); // output // Array ( [1] => Banana )
$fruit1 = array("red"=>"Apple","yellow"=>"Banana","orange"=>"Orange"); $fruit2 = array("yellow"=>"Pear","red"=>"Apple","purple"=>"Grape"); $fruit3 = array("green"=>"Watermelon","orange"=>"Orange","red"=>"Apple"); $intersection = array_diff_assoc($fruit1, $fruit2, $fruit3); print_r($intersection); // output // Array ( [yellow] => Banana )
$fruits = array("apple"=>"red", "banana"=>"yellow"); while ($key = key($fruits)) { printf("%s <br />", $key); next($fruits); } // apple // banana
$fruits = array("apple"=>"red", "banana"=>"yellow"); while ($fruit = current($fruits)) { printf("%s <br />", $fruit); next($fruits); } // red // yellow
$fruits = array("apple", "banana", "orange", "pear"); print_r ( each($fruits) ); // Array ( [1] => apple [value] => apple [0] => 0 [key] => 0 ) $fruits = array("apple", "banana", "orange", "pear"); reset($fruits); while (list($key, $val) = each($fruits)) { echo "$key => $val<br />"; } // 0 => apple // 1 => banana // 2 => orange // 3 => pear
$fruits["apple"] = "red"; $fruits["banana"] = "yellow"; $fruits["watermelon"]="green"; $founded = array_search("green", $fruits); if($founded) printf("%s was founded on %s.",$founded, $fruits[$founded]); //watermelon was founded on green.
$fruits["apple"] = "red"; $fruits["banana"] = "yellow"; $fruits["watermelon"]="green"; $keys = array_keys($fruits); print_r($keys); //Array ( [0] => apple [1] => banana [2] => watermelon )
$fruits["apple"] = "red"; $fruits["banana"] = "yellow"; $fruits["watermelon"]="green"; $values = array_values($fruits); print_r($values); //Array ( [0] => red [1] => yellow [2] => green )
$fruits = array("apple","banana"); array_unshift($fruits,"orange","pear") ; print_r($fruits); // $fruits = array("orange","pear","apple","banana");
$fruits = array("apple","banana"); array_push($fruits,"orange","pear"); print_r($fruits); //$fruits = array("apple","banana","orange","pear")
$fruits = array("apple","banana","orange","pear"); $fruit = array_shift($fruits); print_r($fruits); print_r($fruit); // $fruits = array("banana","orange","pear") // $fruit = "apple";
$fruits = array("apple","banana","orange","pear"); $fruit = array_pop($fruits); print_r($fruits); print_r($fruit); //$fruits = array("apple","banana","orange"); //$fruit = "pear";