本文记录一些PHP数组处理的方法。
1. 检查数组中是否存在某个值 in_array (mixed $needle
, array $haystack
, bool $strict
= false
): bool
检查数组中是否存在某个值
如果 strict
的值为 true
,还会检查 needle
的类型 是否和 haystack
中的相同。
举例:
1 2 3 4 5 6 7 8 <?php $array1 = ['one' , 'two' , 'three' , 'four' , 'five' ];if (in_array("one" , $array1 )) { echo "Got one" ; } ?>
结果:
2. 检查数组或者变量方法 变量检查:
数组检查:
3. 过滤数组元素 array_filter (array $array
, ?callable $callback
= null
, int $mode
= 0): array
array
数组中的每个值会传递给 callback
回调函数,如果 callback
回调函数返回 true
,则将 array
数组中的当前值返回到结果 array 数组中。
如果不设置 callback
回调函数,会删除数组 array
中的所有“空”元素。
过滤后的array 数组键名不会重新索引,可以使用 array_values() 函数对数组进行重新索引。
$mode = 0
- 默认值,接受值作为唯一参数
$mode = ARRAY_FILTER_USE_KEY
- 接受键名作为唯一参数
$mode = ARRAY_FILTER_USE_BOTH
- 同时接受键名和键值
举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 <?php $array1 = [0 => '' , 1 => 'one' , 2 => 'two' , 3 => 'three' , 4 => 'four' , 5 => 'five' ];$array1 = array_filter($array1 );echo "<pre>" ;print_r($array1 ); echo "</pre>" ;$array1 = array_values($array1 ); echo "<pre>" ;print_r($array1 ); echo "</pre>" ;function odd ($var ) { return $var & 1 ; } function even ($var ) { return !($var & 1 ); } $array1 = ['one' => 1 , 'two' => 2 , 'three' => 3 , 'four' => 4 , 'five' => 5 ];$array_even = array_filter($array1 , "even" );$array_odd = array_filter($array1 , "odd" );echo "<pre>" ;print_r($array_even ); echo "</pre>" ;echo "<pre>" ;print_r($array_odd ); echo "</pre>" ;$array2 = [1 => 'one' , 2 => 'two' , 3 => 'three' , 4 => 'four' , 5 => 'five' ];$array_even = array_filter($array2 ,function ($var ) { return (!($var & 1 ));},ARRAY_FILTER_USE_KEY);$array_odd = array_filter($array2 ,function ($var ) { return ($var & 1 );},ARRAY_FILTER_USE_KEY);echo "<pre>" ;print_r($array_even ); echo "</pre>" ;echo "<pre>" ;print_r($array_odd ); echo "</pre>" ;?>
结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 Array ( [1 ] => one [2 ] => two [3 ] => three [4 ] => four [5 ] => five ) Array ( [0 ] => one [1 ] => two [2 ] => three [3 ] => four [4 ] => five ) Array ( [two] => 2 [four] => 4 ) Array ( [one] => 1 [three] => 3 [five] => 5 ) Array ( [2 ] => two [4 ] => four ) Array ( [1 ] => one [3 ] => three [5 ] => five )
4. 删除数组元素 ① array_pop (array &$array
) - 删除数组最后一个元素
举例:
1 2 3 4 5 6 7 8 9 10 <?php $array1 = ['one' , 'two' , 'three' , 'four' , 'five' ];$last = array_pop($array1 );echo "<pre>" ;print_r($array1 ); echo "</pre>" ;?>
结果:
1 2 3 4 5 6 7 Array ( [0 ] => one [1 ] => two [2 ] => three [3 ] => four )
② array_shift (array &$array
) - 将数组开头的单元移出数组并作为结果返回
举例:
1 2 3 4 5 6 7 8 9 10 <?php $array1 = ['one' , 'two' , 'three' , 'four' , 'five' ];$first = array_shift($array1 );echo "<pre>" ;print_r($array1 ); echo "</pre>" ;?>
结果:
1 2 3 4 5 6 7 Array ( [0 ] => two [1 ] => three [2 ] => four [3 ] => five )
5. 插入数组元素 ① array_unshift (&$array
, ...$values
) - 在数组开头插入一个或多个元素
举例:
1 2 3 4 5 6 7 8 9 <?php $array1 = ['one' , 'two' , 'three' , 'four' , 'five' ];array_unshift($array1 , '0' , 'zero' ); echo "<pre>" ;print_r($array1 ); echo "</pre>" ;?>
结果:
1 2 3 4 5 6 7 8 9 10 Array ( [0 ] => 0 [1 ] => zero [2 ] => one [3 ] => two [4 ] => three [5 ] => four [6 ] => five )
② array_push (&$array
, ...$values
) - 将一个或多个单元压入数组的末尾(入栈)
建议直接使用 $array[] =
追加元素。
举例:
1 2 3 4 5 6 7 8 9 10 <?php $array1 = ['one' , 'two' , 'three' , 'four' , 'five' ];array_push($array1 , 'six' , 'seven' ); $array1 [] = 'eight' ;echo "<pre>" ;print_r($array1 ); echo "</pre>" ;?>
结果:
1 2 3 4 5 6 7 8 9 10 11 Array ( [0 ] => one [1 ] => two [2 ] => three [3 ] => four [4 ] => five [5 ] => six [6 ] => seven [7 ] => eight )
③ array_merge (...$arrays
) — 合并一个或多个数组
举例:
1 2 3 4 5 6 7 8 9 10 11 <?php $array1 = ['one' , 'two' , 'three' ];$array2 = ['four' , 'five' ];$array3 = array_merge($array1 , $array2 );$array1 [] = 'eight' ;echo "<pre>" ;print_r($array3 ); echo "</pre>" ;?>
结果:
1 2 3 4 5 6 7 8 Array ( [0 ] => one [1 ] => two [2 ] => three [3 ] => four [4 ] => five )
6. 返回数组最后一个元素 end (&$array
) - 将 array
的内部指针移动到最后一个单元并返回其值。
array_key_last ($array
) - 获取一个数组的最后一个键值
举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <?php $array1 = ['one' , 'two' , 'three' ];$last = end($array1 );echo "<pre>" ;print_r($last ); echo "</pre>" ;echo current($array1 ) . "<br />\n" ; reset($array1 ); echo current($array1 ) . "<br />\n" ;next($array1 ); echo current($array1 ) . "<br />\n" ;prev($array1 ); echo current($array1 ) . "<br />\n" ;$array1 = ['one' , 'two' , 'three' ];$last = array_key_last($array1 );echo "<pre>" ;print_r($last ); echo "</pre>" ; ?>
结果:
1 2 3 4 5 6 three three one two one 2
7. 数组切片 array_slice ( $array
, $offset
, $length
= null
, $preserve_keys
= false
) - 返回根据 offset
和 length
参数所指定的 array
数组中的一段序列。
举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 <?php $array1 = ['one' , 'two' , 'three' , 'four' , 'five' ];$output = array_slice($array1 , 2 ); print_r($output ) ; echo "<br />\n" ;$output = array_slice($array1 , -3 , 2 ); print_r($output ) ; echo "<br />\n" ;$output = array_slice($array1 , 0 , 3 ); print_r($output ) ; echo "<br />\n" ;$output = array_slice($array1 , 1 , -3 ); print_r($output ) ; echo "<br />\n" ;$output = array_slice($array1 , 1 , 3 ); print_r($output ) ; echo "<br />\n" ;$output = array_slice($array1 , 1 , 3 , true ); print_r($output ) ; echo "<br />\n" ;?>
结果:
1 2 3 4 5 6 Array ( [0 ] => three [1 ] => four [2 ] => five )Array ( [0 ] => three [1 ] => four )Array ( [0 ] => one [1 ] => two [2 ] => three )Array ( [0 ] => two )Array ( [0 ] => two [1 ] => three [2 ] => four )Array ( [1 ] => two [2 ] => three [3 ] => four )
8. 判断两个数组是否相等 array_diff ( $array
, ...$arrays
) - 返回在 array
中但是不在其他 array 里的值。可用于判断两个数组是否相等。
array_diff_key ( $array
, ...$arrays
) - 返回在 array
中但是未出现在任何其它数组中的键名的值。
举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 <?php $array1 = ['one' , 'two' , 'three' ];$array2 = ['one' , 'two' , 'three' , 'four' , 'five' ];$output1 = array_diff($array1 , $array2 );$output2 = array_diff($array2 , $array1 );if (empty ($output1 ) and empty ($output2 )) { echo "The arrays are equal" ; } else { echo "The arrays are not equal" ; } ?>
结果:
1 The arrays are not equal
9. 替换数组元素 array_replace ( $array
, ...$replacements
) - 使用传递的数组替换第一个数组的元素
举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?php $array1 = ['one' , 'two' , 'three' ];$array2 = ['four' , 'five' , 'six' ];$output = array_replace($array1 , $array2 );echo "<pre>" ;print_r($output ); echo "</pre>" ;$array1 = ['one' , 'two' , 'three' , '' , '' , '' ];$array2 = ['' , '' , '' , 'four' , 'five' , 'six' ];$output = array_replace($array1 ,array_slice($array2 ,3 ,null ,$preserve_keys = true ));echo "<pre>" ;print_r($output ); echo "</pre>" ;?>
结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Array ( [0 ] => four [1 ] => five [2 ] => six ) Array ( [0 ] => one [1 ] => two [2 ] => three [3 ] => four [4 ] => five [5 ] => six )
10. 移除数组重复元素 array_unique - 移除数组中重复的值
举例:
1 2 3 4 5 6 7 8 9 <?php $array1 = ['one' , 'two' , 'three' , 'four' , 'five' , 'five' ];$output = array_unique($array1 );echo "<pre>" ;print_r($output ); echo "</pre>" ;?>
结果:
1 2 3 4 5 6 7 8 Array ( [0 ] => one [1 ] => two [2 ] => three [3 ] => four [4 ] => five )
11. 数组排序 常用排序函数如下表:
例1:asort
1 2 3 4 5 6 7 8 <?php $array1 = ['two' => 2 , 'one' => 1 , 'four' => 4 , 'three' => 3 , 'five' => 5 ];asort($array1 ); echo "<pre>" ;print_r($array1 ); echo "</pre>" ;?>
结果:
1 2 3 4 5 6 7 8 Array ( [one] => 1 [two] => 2 [three] => 3 [four] => 4 [five] => 5 )
例2:ksort
1 2 3 4 5 6 7 $array1 = [1 => 'one' , 3 => 'three' , 2 => 'two' , 5 => 'five' , 4 => 'four' ];ksort($array1 ); echo "<pre>" ;print_r($array1 ); echo "</pre>" ;?>
结果:
1 2 3 4 5 6 7 8 Array ( [1 ] => one [2 ] => two [3 ] => three [4 ] => four [5 ] => five )
例3:array_multisort()多维数组排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 $records = array ( array ( 'id' => 2 , 'name' => 'zhangsan' , ), array ( 'id' => 1 , 'name' => 'lishi' , ), array ( 'id' => 3 , 'name' => 'wanger' , ) ); $ids = array_column($records , 'id' );array_multisort($ids , SORT_DESC, $records ); echo "<xmp class='a-left'>" ;print_r($records ); echo "</xmp>" ;
结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Array ( [0 ] => Array ( [id] => 3 [name] => wanger ) [1 ] => Array ( [id] => 2 [name] => zhangsan ) [2 ] => Array ( [id] => 1 [name] => lishi ) )
12.读取多维数组某一列的值 array_column 方法可用来读取多维数组或对象数组某一列的值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 $records = array ( array ( 'id' => 1 , 'name' => 'zhangsan' , ), array ( 'id' => 2 , 'name' => 'lishi' , ), array ( 'id' => 3 , 'name' => 'wanger' , ) ); echo "<xmp class='a-left'>" ;print_r($records ); echo "</xmp>" ;$names = array_column($records , 'name' , 'id' );echo "<xmp class='a-left'>" ;print_r($names ); echo "</xmp>" ;
结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 Array ( [0 ] => Array ( [id] => 1 [name] => zhangsan ) [1 ] => Array ( [id] => 2 [name] => lishi ) [2 ] => Array ( [id] => 3 [name] => wanger ) ) Array ( [1 ] => zhangsan [2 ] => lishi [3 ] => wanger )
13.统计数组某一列的值 例如有一个这样的数组:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 $array = [ ['id' => 1 , 'name' => 'one' ], ['id' => 2 , 'name' => 'two' ], ['id' => 1 , 'name' => 'one' ], ['id' => 2 , 'name' => 'two' ], ['id' => 3 , 'name' => 'three' ] ]; $array = [ (object )['id' => 1 , 'name' => 'one' ], (object )['id' => 2 , 'name' => 'two' ], (object )['id' => 1 , 'name' => 'one' ], (object )['id' => 2 , 'name' => 'two' ], (object )['id' => 3 , 'name' => 'three' ] ];
使用 array_count_values
方法统计name列的值:
1 2 3 4 5 6 7 8 9 10 $count = array_count_values(array_column($array , 'name' ));print_r($count ); Array ( [one] => 2 [two] => 2 [three] => 1 )
参考资料:
数组排序:https://www.php.net/manual/zh/array.sorting.php
https://www.php.net/manual/zh/book.array.php
--THE END--
凡事都应当尽可能地简单,而不是较为简单。 ——爱因斯坦