功能:用一个数组的值作为新数组的键名,另一个数组的值作为新数组的值
案例:
<?php $a = array("one","two","three"); $b = array("一","二","三"); $c = array_combine($a,$b); print_r($c); /**结果 *Array ( [one] => 一 [two] => 二 [three] => 三 ) */
array_chunk
功能:拆分数组成多个数组
<?php $input_array = array("a"=>"apple","b"=>"blue","c","d","e"); echo "<pre>"; print_r(array_chunk($input_array, 2)); print_r(array_chunk($input_array, 2,True)); echo "</pre>"; /**结果 Array ( [0] => Array ( [0] => apple [1] => blue ) [1] => Array ( [0] => c [1] => d ) [2] => Array ( [0] => e ) ) Array ( [0] => Array ( [a] => apple [b] => blue ) [1] => Array ( [0] => c [1] => d ) [2] => Array ( [2] => e ) ) */
array_count_values
功能:统计数组中值出现的次数
<?php $input_array = array("a"=>"apple","b"=>"blue","c","d","e"); echo "<pre>"; print_r(array_count_values($input_array)); echo "</pre>"; /**结果 Array ( [apple] => 1 [blue] => 1 [c] => 1 [d] => 1 [e] => 1 ) */
array_diff
功能:第一个数组中去掉第二个数组中有的数据,返回剩下的内容作为结果
<?php $array1 = array("a"=>"apple","b"=>"blue","c","d","e"); $array2 = array("apple","c","d","f"); $result = array_diff($array1, $array2); $result2 = array_diff($array2, $array1); echo "<pre>"; print_r($result);//数组1中去掉数组2中剩下的 print_r($result2);//数组2中去掉数组1中剩下的 echo "</pre>"; /**结果 Array ( [b] => blue [2] => e ) Array ( [3] => f ) */
array_map
功能:将回调函数执行到数组中
<?php //定义回调函数 function cube($n){ return ($n*$n*$n); } $a = array(1,2,3,4,5); $b = array_map("cube",$a); echo "<pre>"; print_r($b); echo "</pre>"; /**结果 Array ( [0] => 1 [1] => 8 [2] => 27 [3] => 64 [4] => 125 ) */
array_merge
功能:合并一个或多个数组
说明:如果后面有键名相同的会覆盖掉前面的内容,键名为数字的会添加到后面
<?php $array1 = array("color"=>"red",2,4); $array2 = array("a","b","color"=>"green","shape"=>"trapezoid",4); $result1 = array_merge($array1,$array2); $result2 = array_merge_recursive($array1,$array2); echo "<pre>"; print_r($result1); print_r($result2); echo "</pre>"; /**结果 Array ( [color] => green [0] => 2 [1] => 4 [2] => a [3] => b [shape] => trapezoid [4] => 4 ) Array ( [color] => Array ( [0] => red [1] => green ) [0] => 2 [1] => 4 [2] => a [3] => b [shape] => trapezoid [4] => 4 ) */
array_pop
功能:剔除数组最后一个元素,返回被剔除的元素内容
<?php $stack = array("orange","banana","apple","1"); $last = array_pop($stack); echo "<pre>"; print_r($stack); print_r($last); echo "</pre>"; /**结果 Array ( [0] => orange [1] => banana [2] => apple ) 1 */
array_push
功能:将一个多个单元压入数组末尾,返回之后的数组个数
<?php $stack = array("orange","banana"); $count = array_push($stack,"apple","red","blue"); echo "<pre>"; print_r($stack); print_r($count); echo "</pre>"; /**结果 Array ( [0] => orange [1] => banana [2] => apple [3] => red [4] => blue ) 5 */
array_rand
功能:获取随机的键名
<?php $input = array("orange","banana","apple","red","blue"); $rand = array_rand($input,2);; print_r($rand); $rand = array_rand($input,3); print_r($rand); /**结果 Array ( [0] => 1 [1] => 4 ) Array ( [0] => 0 [1] => 1 [2] => 3 ) */
array_search
功能:查询数组中的内容,返回键值,如果有多个匹配,返回第一个匹配的内容
<?php $array = array("blue"=>"b","red"=>"r","green","r"); $key = array_search('b', $array); echo $key; echo "<br>"; $key = array_search('r', $array); echo $key; echo "<br>"; /**结果 blue red */
array_shift
功能:移除开头的元素,与array_pop相反
<?php $fruit = array("milk","orange","banana","apple"); $top = array_shift($fruit); print_r($top); echo "<br>"; print_r($fruit); /**结果 milk Array ( [0] => orange [1] => banana [2] => apple ) */
array_unique
功能:去除数组重复的元素,保留第一个出现的,包括键名和值