PHP常用数组函数
PHP 数组函数
list() 提取数组中多个值
void list(mixed …)
将数组 $sales 值赋给其它变量,
相当于 $a = b =c =$sales[2],
$sales = [
['Northeast', date('m-d'), 12.54],
['Northwest', date('m-d'), 563.90],
['Southeast', date('m-d'), 16.12],
];
list($a, $b, $c) = $sales;
range() 用预定义的值生成数组
array range (int low, int high [, int step])
$die = range(1, 6, 2);
$list = range('A', 'Z');
输出:
Array ( [0] => 1 [1] => 3 [2] => 5 )
Array ( [0] => A [1] => B [2] => C [3] => D [4] => E …)
is_array() 检查是否数组
boolean is_array(mixed variable)
$string = "string is an array: ", is_array('string') ? 'true' : 'false';
输出:
string is an array: false
foreach() 输出数组
foreach ($sales as $sale_v){
print_r($sale_v);
}
array_unshift() 在数组头添加元素
$color = ['red', 'blue', 'green'];
array_unshift($color, 'yellow','pink');
输出:
Array ( [0] => yellow [1] => pink [2] => red [3] => blue [4] => green )
array_push() 在数组末尾添加元素
array_push($color, 'white','black');
输出:
Array ( [0] => yellow [1] => pink [2] => red [3] => blue [4] => green [5] => white )
array_pop() 从数组末尾删除元素
array_pop($color);
输出:
Array ( [0] => yellow [1] => pink [2] => red [3] => blue [4] => green [5] => white )
in_array() 搜索在数组是否存在某个值
$states = in_array($color, ['orange', 'tan']);
输出: bool(false)
array_search() 搜索数组中是否存在某个值,并返回对应的键
array_search('blue', $color);
输出: 3
array_keys() 获取数组键, array_values() 获取数组值
$fruit = ['apple' => 'red', 'banana' => 'yellow'];
array_keys($fruit)
array_values($fruit)
输出:
Array ( [0] => apple [1] => banana )
Array ( [0] => red [1] => yellow )
array_walk() 向数组传入自定义函数
数组首字母大写
function ucword(&$value, $key){
$value = ucwords($value);
}
array_walk($color, "ucword");
输出:
Array ( [0] => Red [1] => Blue [2] => Green )
array_count_values() 统计数组元素出现的次数
$color = ['red', 'blue', 'green', 'red', 'blue', 'blue'];
$count = array_count_values($color);
输出:
Array ( [red] => 2 [blue] => 3 [green] => 1 )
array_unique() 剔除重复数据
$color = ['red', 'blue', 'green', 'red', 'blue', 'blue'];
$unique = array_unique($color);
输出:
Array ( [0] => red [1] => blue [2] => green )
array_flip() 交换数组的键和值
$fruit = ['apple' => 'red', 'banana' => 'yellow'];
$flip = array_flip($fruit);
输出:
Array ( [red] => apple [yellow] => banana )
array_merge() 合并数组
$merge = array_merge($color, $fruit);
输出:
Array ( [0] => red [1] => blue [2] => green [apple] => red [banana] => yellow )
array_combine 合并两个数组,生成的新数组以数组1作key数组2作值
$mysql_key = ['a', 'b', 'c', 'd', 'e', 'f'];
$mysql_val = [1, 2, 3, 4, 5, 6];
$combine = array_combine($mysql_key, $mysql_val);
Array ( [a] => 1 [b] => 2 [c] => 3 [d] => 4 [e] => 5 [f] => 6 )
array_slice() 截取数组的一部分
截取前3的元素,
截取第3个之后的元素
$slice = array_slice($combine, 0, 3);
$slice2 = array_slice($combine, 3);
输出:
Array ( [a] => 1 [b] => 2 [c] => 3 )
Array ( [d] => 4 [e] => 5 [f] => 6 )
array_intersect() 求数组交集
array_intersect_assoc() 用于求关联数组交集,不同点是会要求键也一样
$intersect = array_intersect(['GZ', 'BJ', 'TJ'] , ['SH', 'SZ', 'GZ']);
输出:
Array ( [0] => GZ )
array_diff() 求数组差集
与 array_intersect() 函数功能相反
array_diff_assoc 求关联数组差集
$diff = array_diff(['GZ', 'BJ', 'TJ'] , ['SH', 'SZ', 'GZ']);
输出:
Array ( [1] => BJ [2] => TJ )
shuffle() 随机打乱数组排序
$mysql_key = ['a', 'b', 'c', 'd', 'e', 'f'];
shuffle($mysql_key);
输出:
Array ( [0] => e [1] => a [2] => b [3] => f [4] => c [5] => d )
array_sum() 数组求和
$mysql_val = [1, 2, 3, 4, 5, 6];
$sum = array_sum($mysql_val);
输出: 21
下一篇: PHP常用数组函数