收藏的PHP数组函数
目录
类型
索引数组
带有数字索引的数组
$cars=array("Volvo","BMW","SAAB");
// anothor ways
$cars[0]="Volvo";
$cars[1]="BMW";
$cars[2]="SAAB";
关联数组
使用您分配给数组的指定键的数组。
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
// another ways
$age['Peter']="35";
$age['Ben']="37";
$age['Joe']="43";
多维数组
包含一个或多个数组的数组。
$cars = array
(
array("Volvo",22,18),
array("BMW",15,13),
);
// another ways
$cars[0][0] = 'Volvo' ;
$cars[0][1] = 22;
$cars[0][2] = 18 ;
$cars[1][0] = 'BMW';
$cars[1][0] = 15;
$cars[1][0] = 13;
取值
输出数组中当前元素的值
$people = array("Bill", "Steve", "Mark", "David");
echo current($people); // 当前元素是 Bill
输出数组中最后一个元素的值
$people = array("Bill", "Steve", "Mark", "David");
echo end($people); // 最后一个元素是 David
输出数组中下一个元素的值
$people = array("Bill", "Steve", "Mark", "David");
echo next($people); // Bill 的下一个元素是 Steve
输出数组中上一个元素的值
$people = array("Bill", "Steve", "Mark", "David");
echo prev($people); // David 之前的元素是 Mark
输出数组中第一个元素的值
$people = array("Bill", "Steve", "Mark", "David");
echo reset($people); // 把内部指针移动到数组的首个元素,即 Bill
输出数组中当前元素的键名和键值,并将内部指针向前移动
$people = array("Bill", "Steve", "Mark", "David");
each($people); // 返回当前元素的键名和键值,即'0'=> Bill
echo current($people); // 并将内部指针向前移动, 即Steve
输出数组中当前元素键名
$a = array('key'=>'value');
echo key($a); // key
返回数组中部分的或所有的键名
$array = array(0 => 100, "color" => "red");
$result = array_keys($array);
print_r($result); // Array ( [0] => 0 [1] => color )
返回数组中所有的值
$array = array("size" => "XL", "color" => "gold");
$result = array_values($array);
print_r($result); // Array ( [0] => XL [1] => gold )
返回单元顺序相反的数组
$input = array("php", 4.0, array("green", "red"));
$reversed = array_reverse($input);
print_r($reversed); // Array ( [0] => Array ( [0] => green [1] => red ) [1] => 4 [2] => php )
返回数组中指定的一列
// Array representing a possible record set returned from a database
$records = array(
array(
'id' => 2135,
'first_name' => 'John',
'last_name' => 'Doe',
),
array(
'id' => 3245,
'first_name' => 'Sally',
'last_name' => 'Smith',
),
array(
'id' => 5342,
'first_name' => 'Jane',
'last_name' => 'Jones',
),
array(
'id' => 5623,
'first_name' => 'Peter',
'last_name' => 'Doe',
)
);
$first_names = array_column($records, 'first_name');
print_r($first_names); // Array ( [0] => John [1] => Sally [2] => Jane [3] => Peter )
从数组中随机取出一个或多个单元
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
print_r($rand_keys); // Array ( [0] => 2 [1] => 4 )
echo $input[$rand_keys[0]]; // Neo
echo $input[$rand_keys[1]]; // Tank
操作
将数组中的所有键名修改为全大写或小写
$input_array = array("FirSt" => 1, "SecOnd" => 4);
// upper
$case_upper = array_change_key_case($input_array, CASE_UPPER);
print_r($case_upper); // Array ( [FIRST] => 1 [SECOND] => 4 )
// lower
$case_lower= array_change_key_case($input_array, CASE_LOWER);
print_r($case_lower); // Array ( [first] => 1 [second] => 4 )
从数组中取出一段
$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"
把数组中的值赋给一组变量
$info = array('coffee', 'brown', 'caffeine');
list($drink, $color, $power) = $info;
echo "$drink is $color and $power makes it special."; // coffee is brown and caffeine makes it special.
建立一个包含指定范围单元的数组
$number = range(0, 5);
print_r($number); // Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 )
去掉数组中的某一部分并用其它值取代
$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, 1, "orange");
print_r($input); //Array ( [0] => red [1] => orange [2] => blue [3] => yellow )
在数组中搜索给定的值,如果成功则返回首个相应的键名
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array);
print_r($key); // 2
打乱数组
$number = range(0, 5);
shuffle($number);
print_r($number); // Array ( [0] => 2 [1] => 4 [2] => 0 [3] => 5 [4] => 3 [5] => 1 )
移除数组中重复的值
$array = array(1, "hello", 1, "world", "hello");
$result = array_unique($array);
print_r($result); // Array ( [0] => 1 [1] => hello [3] => world )
将一个数组分割成多个
$input_array = array('a', 'b', 'c', 'd', 'e');
$output_array = array_chunk($input_array, 2);
print_r($output_array);
// Array ( [0] => Array ( [0] => a [1] => b ) [1] => Array ( [0] => c [1] => d ) [2] => Array ( [0] => e ) )
交换数组中的键和值
$input = array("oranges", "apples", "pears");
$flipped = array_flip($input);
print_r($flipped); // Array ( [oranges] => 0 [apples] => 1 [pears] => 2 )
创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值
$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 )
创建包含变量名和它们的值的数组
$firstname = "Bill";
$lastname = "Gates";
$age = "60";
$result = compact("firstname", "lastname", "age");
print_r($result); // Array ( [firstname] => Bill [lastname] => Gates [age] => 60 )
从数组中将变量导入到当前的符号表
$var_array = array("color" => "blue",
"size" => "medium",
"shape" => "sphere");
extract($var_array);
echo "$color, $size, $shape"; // blue, medium, sphere
排序
对数组排序,由低到高
// Array to be sorted
$fruits = array("lemon", "orange", "banana", "apple");
// Sort and print the resulting array
sort($fruits);
print_r($fruits); // Array ( [0] => apple [1] => banana [2] => lemon [3] => orange )
对数组逆向排序,由高到低
// Array to be sorted
$fruits = array("lemon", "orange", "banana", "apple");
// Sort and print the resulting array
rsort($fruits);
print_r($fruits); // Array ( [0] => orange [1] => lemon [2] => banana [3] => apple )
对数组按照键名排序,由低到高
// Array to be sorted
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
// Sort and print the resulting array
ksort($fruits);
print_r($fruits); // Array ( [a] => orange [b] => banana [c] => apple [d] => lemon )
对数组按照键名逆向排序,由高到低
// Array to be sorted
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
// Sort and print the resulting array
krsort($fruits);
print_r($fruits); // Array ( [d] => lemon [c] => apple [b] => banana [a] => orange )
对数组进行排序并保持索引关系,由低到高
// Array to be sorted
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
// Sort and print the resulting array
asort($fruits);
print_r($fruits); // Array ( [c] => apple [b] => banana [d] => lemon [a] => orange )
对数组进行逆向排序并保持索引关系,由高到低
// Array to be sorted
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
// Sort and print the resulting array
arsort($fruits);
print_r($fruits); // Array ( [a] => orange [d] => lemon [b] => banana [c] => apple )
使用用户自定义的比较函数对数组中的值进行排序
function cmp($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
// Array to be sorted
$a = array(3, 2, 5, 6, 1);
// Sort and print the resulting array
usort($a, "cmp");
print_r($a); // Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 5 [4] => 6 )
使用用户自定义的比较函数对数组中的值进行排序并保持索引关联
function cmp($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
// Array to be sorted
$array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
// Sort and print the resulting array
uasort($array, 'cmp');
print_r($array); // Array ( [d] => -9 [h] => -4 [c] => -1 [e] => 2 [g] => 3 [a] => 4 [f] => 5 [b] => 8 )
使用用户自定义的比较函数对数组中的键名进行排序
function cmp($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
// Array to be sorted
$array = array('f' => 5, 'a' => 4, 'h' => -4, 'd' => -9, 'c' => -1, 'e' => 2, 'g' => 3, 'b' => 8);
// Sort and print the resulting array
uksort($array, 'cmp');
print_r($array); // Array ( [a] => 4 [b] => 8 [c] => -1 [d] => -9 [e] => 2 [f] => 5 [g] => 3 [h] => -4 )
用“自然排序”算法对数组排序
// Array to be sorted
$array1 = $array2 = array("img12.png", "img10.png", "img2.png", "img1.png");
// Standard sorting
asort($array1);
print_r($array1); // Array ( [3] => img1.png [1] => img10.png [0] => img12.png [2] => img2.png )
//nNatural order sorting
natsort($array2);
print_r($array2); // Array ( [3] => img1.png [2] => img2.png [1] => img10.png [0] => img12.png )
用“自然排序”算法对数组进行不区分大小写字母的排序
// Array to be sorted
$array1 = $array2 = array('IMG0.png', 'img12.png', 'img10.png', 'img2.png', 'img1.png', 'IMG3.png');
// Standard sorting
sort($array1);
print_r($array1);
// Array ( [0] => IMG0.png [1] => IMG3.png [2] => img1.png [3] => img10.png [4] => img12.png [5] => img2.png )
// nNatural order sorting (case-insensitive)
natcasesort($array2);
print_r($array2);
// Array ( [0] => IMG0.png [4] => img1.png [3] => img2.png [5] => IMG3.png [2] => img10.png [1] => img12.png )
对多个数组或多维数组进行排序
$arr = array(
array(
'top' => 0,
'cnt' => 20,
),
array(
'top' => 0,
'cnt' => 10,
),
array(
'top' => 1,
'cnt' => 30,
),
);
array_multisort(array_column($arr, 'top'), SORT_DESC, SORT_NUMERIC,
array_column($arr, 'cnt'), SORT_ASC, SORT_NUMERIC,
$arr);
print_r($arr); // Array ( [0] => Array ( [top] => 1 [cnt] => 30 ) [1] => Array ( [top] => 0 [cnt] => 10 ) [2] => Array ( [top] => 0 [cnt] => 20 ) )
计算
统计数组中元素数量
$a = array(0,1,2,3);
echo count($a); // 4
统计数组中所有的值
$array = array(1, "hello", 1, "world", "hello");
$result = array_count_values($array);
print_r($result); // Array ( [1] => 2 [hello] => 2 [world] => 1 )
对数组中所有值求和
$a = array(2, 4, 6, 8);
$sum = array_sum($a);
echo "sum(a) = " . $sum; // sum(a) = 20
计算数组中所有值的乘积
$a = array(2, 4, 6, 8);
$result = array_product($a);
print_r($result); // 384
检查
检查数组里是否有指定的键名或索引
$search_array = array('first' => 1, 'second' => 4);
echo array_key_exists('first', $search_array); // 1
检查数组中是否存在某个值
$os = array("Mac", "NT", "Irix", "Linux");
echo in_array("Irix", $os); // 1
合并
合并一个或多个数组
$array1 = array("color" => "red", 2, 4);
$array2 = 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 )
递归地合并一个或多个数组
$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);
print_r($result); // Array ( [color] => Array ( [favorite] => Array ( [0] => red [1] => green ) [0] => blue ) [0] => 5 [1] => 10 )
替换
使用传递的数组替换第一个数组的元素
$base = array("orange", "banana", "apple", "raspberry");
$replacements = array(0 => "pineapple", 4 => "cherry");
$replacements2 = array(0 => "grape");
$basket = array_replace($base, $replacements, $replacements2);
print_r($basket); // Array ( [0] => grape [1] => banana [2] => apple [3] => raspberry [4] => cherry )
使用传递的数组递归替换第一个数组的元素
$base = array('citrus' => array( "orange") , 'berries' => array("blackberry", "raspberry"), );
$replacements = array('citrus' => array('pineapple'), 'berries' => array('blueberry'));
$basket = array_replace_recursive($base, $replacements);
print_r($basket); // Array ( [citrus] => Array ( [0] => pineapple ) [berries] => Array ( [0] => blueberry [1] => raspberry )
$basket = array_replace($base, $replacements);
print_r($basket); // Array ( [citrus] => Array ( [0] => pineapple ) [berries] => Array ( [0] => blueberry ) )
填充
使用指定的键和值填充数组
$keys = array('foo', 5, 10, 'bar');
$a = array_fill_keys($keys, 'banana');
print_r($a); // Array ( [foo] => banana [5] => banana [10] => banana [bar] => banana )
用给定的值填充数组
$a = array_fill(5, 3, 'banana');
print_r($a); // Array ( [5] => banana [6] => banana [7] => banana )
以指定长度将一个值填充进数组
$input = array(12, 10, 9);
$result = array_pad($input, 5, 0);
print_r($result); // Array ( [0] => 12 [1] => 10 [2] => 9 [3] => 0 [4] => 0 )
堆栈
将数组开头的单元移出数组
stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_shift($stack);
print_r($stack); // Array ( [0] => banana [1] => apple [2] => raspberry )
在数组开头插入一个或多个单元
$queue = array("orange", "banana");
array_unshift($queue, "apple", "raspberry");
print_r($queue); // Array ( [0] => apple [1] => raspberry [2] => orange [3] => banana )
弹出数组最后一个单元(出栈)
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_pop($stack);
print_r($stack); // Array ( [0] => orange [1] => banana [2] => apple )
将一个或多个单元压入数组的末尾(入栈)
$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");
print_r($stack); // Array ( [0] => orange [1] => banana [2] => apple [3] => raspberry )
回调
使用用户自定义函数对数组中的每个元素做回调处理
$fruits = array("d" => "lemon", "a" => "orange");
function test_alter(&$item1, $key, $prefix)
{
$item1 = "$prefix: $item1";
}
array_walk($fruits, 'test_alter', 'fruit');
print_r($fruits); // Array ( [d] => fruit: lemon [a] => fruit: orange )
对数组中的每个成员递归地应用用户函数
$sweet = array('a' => 'apple', 'b' => 'banana');
$fruits = array('sweet' => $sweet, 'sour' => 'lemon');
function test_alter(&$item1, $key, $prefix)
{
$item1 = "$prefix: $item1";
}
array_walk_recursive($fruits, 'test_alter','fruit');
print_r($fruits); // Array ( [sweet] => Array ( [a] => fruit: apple [b] => fruit: banana ) [sour] => fruit: lemon )
为数组的每个元素应用回调函数
function cube($n)
{
return($n * $n * $n);
}
$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
print_r($b); // Array ( [0] => 1 [1] => 8 [2] => 27 [3] => 64 [4] => 125 )
用回调函数迭代地将数组简化为单一的值
function sum($carry, $item)
{
$carry += $item;
return $carry;
}
$a = array(1, 2, 3, 4, 5);
$result = array_reduce($a, "sum");
print_r($result); // 15
用回调函数过滤数组中的单元
function odd($var)
{
// returns whether the input integer is odd
return($var & 1);
}
function even($var)
{
// returns whether the input integer is even
return(!($var & 1));
}
$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
$array2 = array(6, 7, 8, 9, 10, 11, 12);
print_r(array_filter($array1, "odd")); // Array ( [a] => 1 [c] => 3 [e] => 5 )
print_r(array_filter($array2, "even")); // Array ( [0] => 6 [2] => 8 [4] => 10 [6] => 12 )
差集
计算数组的差集
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result); // Array ( [b] => brown [c] => blue )
用回调函数比较数据来计算数组的差集
function value_compare_func($key1, $key2)
{
if ($key1 === $key2)
return 0;
else if ($key1 > $key2)
return 1;
else
return -1;
}
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_udiff($array1, $array2, "value_compare_func");
print_r($result); // Array ( [b] => brown [c] => blue )
带索引检查计算数组的差集
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_assoc($array1, $array2);
print_r($result); // Array ( [b] => brown [c] => blue [0] => red )
用用户提供的回调函数做索引检查来计算数组的差集
function key_compare_func($a, $b)
{
if ($a === $b) {
return 0;
}
return ($a > $b)? 1:-1;
}
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_uassoc($array1, $array2, "key_compare_func");
print_r($result); // Array ( [b] => brown [c] => blue [0] => red )
带索引检查计算数组的差集,用回调函数比较数据
function value_compare_func($key1, $key2)
{
if ($key1 === $key2)
return 0;
else if ($key1 > $key2)
return 1;
else
return -1;
}
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_udiff_assoc($array1, $array2, "value_compare_func");
print_r($result); // Array ( [b] => brown [c] => blue [0] => red )
带索引检查计算数组的差集,用回调函数比较数据和索引
function key_compare_func($key1, $key2)
{
if ($key1 === $key2)
return 0;
else if ($key1 > $key2)
return 1;
else
return -1;
}
function value_compare_func($a, $b)
{
if ($a === $b) {
return 0;
}
return ($a > $b)? 1:-1;
}
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_udiff_uassoc($array1, $array2, "value_compare_func", "key_compare_func");
print_r($result); // Array ( [b] => brown [c] => blue [0] => red )
使用键名比较计算数组的差集
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
$result = array_diff_key($array1, $array2);
print_r($result); // Array ( [red] => 2 [purple] => 4 )
用回调函数对键名比较计算数组的差集
function key_compare_func($key1, $key2)
{
if ($key1 === $key2)
return 0;
else if ($key1 > $key2)
return 1;
else
return -1;
}
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "b" => "yellow", "red");
$result = array_diff_uassoc($array1, $array2, "key_compare_func");
print_r($result); // Array ( [b] => brown [c] => blue [0] => red )
交集
计算数组的交集
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result); // Array ( [a] => green [0] => red )
计算数组的交集,用回调函数比较数据
function value_compare_func($a, $b)
{
if ($a === $b) {
return 0;
}
return ($a > $b)? 1:-1;
}
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_uintersect($array1, $array2,'value_compare_func');
print_r($result); // Array ( [a] => green [0] => red )
带索引检查计算数组的交集
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "b" => "yellow", "blue", "red");
$result_array = array_intersect_assoc($array1, $array2);
print_r($result_array); // Array ( [a] => green )
带索引检查计算数组的交集,用回调函数比较索引
function key_compare_func($key1, $key2)
{
if ($key1 === $key2)
return 0;
else if ($key1 > $key2)
return 1;
else
return -1;
}
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "b" => "yellow", "blue", "red");
$result_array = array_intersect_uassoc($array1, $array2,"key_compare_func");
print_r($result_array); // Array ( [a] => green )
带索引检查计算数组的交集,用回调函数比较数据
function value_compare_func($a, $b)
{
if ($a === $b) {
return 0;
}
return ($a > $b) ? 1 : -1;
}
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "b" => "yellow", "blue", "red");
$result_array = array_uintersect_assoc($array1, $array2, "value_compare_func");
print_r($result_array); // Array ( [a] => green )
带索引检查计算数组的交集,用单独的回调函数比较数据和索引
function key_compare_func($key1, $key2)
{
if ($key1 === $key2)
return 0;
else if ($key1 > $key2)
return 1;
else
return -1;
}
function value_compare_func($a, $b)
{
if ($a === $b) {
return 0;
}
return ($a > $b) ? 1 : -1;
}
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "b" => "yellow", "blue", "red");
$result_array = array_uintersect_uassoc($array1, $array2, "value_compare_func", "key_compare_func");
print_r($result_array); // Array ( [a] => green )
使用键名比较计算数组的交集
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
$result = array_intersect_key($array1, $array2);
print_r($result); // Array ( [blue] => 1 [green] => 3 )
用回调函数比较键名来计算数组的交集
function key_compare_func($key1, $key2)
{
if ($key1 === $key2)
return 0;
else if ($key1 > $key2)
return 1;
else
return -1;
}
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
$result = array_intersect_ukey($array1, $array2, "key_compare_func");
print_r($result); // Array ( [blue] => 1 [green] => 3 )