array_chunk
array_chunk — 将一个数组分割成多个
<?php
$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2, true));
?>
以上例程会输出:
Array ( [0] => Array ( [0] => a [1] => b ) [1] => Array ( [0] => c [1] => d ) [2] => Array ( [0] => e ) ) Array ( [0] => Array ( [0] => a [1] => b ) [1] => Array ( [2] => c [3] => d ) [2] => Array ( [4] => e ) )
array_slice
array_slice — 从数组中取出一段
<?php
$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"
// note the differences in the array keys
print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));
?>
array_splice
array_splice — 去掉数组中的某一部分并用其它值取代
<?php
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
// $input is now array("red", "green")
$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, -1);
// $input is now array("red", "yellow")
$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, count($input), "orange");
// $input is now array("red", "orange")
$input = array("red", "green", "blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));
// $input is now array("red", "green",
// "blue", "black", "maroon")
$input = array("red", "green", "blue", "yellow");
array_splice($input, 3, 0, "purple");
// $input is now array("red", "green",
// "blue", "purple", "yellow");
?>
array_column
(PHP 5 >= 5.5.0, PHP 7)
array_column — 返回数组中指定的一列
<?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 )
<?php
// Using the $records array from Example #1
$last_names = array_column($records, 'last_name', 'id');
print_r($last_names);
?>
以上例程会输出:
Array ( [2135] => Doe [3245] => Smith [5342] => Jones [5623] => Doe )
array_combine
(PHP 5, PHP 7)
array_combine — 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值
<?php
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);
print_r($c);
?>
array_merge
(PHP 4, PHP 5, PHP 7)
array_merge — 合并一个或多个数组
<?php
$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 )
array_walk
(PHP 4, PHP 5, PHP 7)
array_walk — 使用用户自定义函数对数组中的每个元素做回调处理
将用户自定义函数 funcname
应用到 array
数组中的每个单元。
array_walk() 不会受到 array
内部数组指针的影响。array_walk() 会遍历整个数组而不管指针的位置。
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
function test_alter(&$item1, $key, $prefix)
{
$item1 = "$prefix: $item1";
}
function test_print($item2, $key)
{
echo "$key. $item2<br />\n";
}
echo "Before ...:\n";
array_walk($fruits, 'test_print');
array_walk($fruits, 'test_alter', 'fruit');
echo "... and after:\n";
array_walk($fruits, 'test_print');
list
(PHP 4, PHP 5, PHP 7)
list — 把数组中的值赋给一组变量
<?php
list($a, list($b, $c)) = array(1, array(2, 3));
var_dump($a, $b, $c);
?>
int(1) int(2) int(3)
each
(PHP 4, PHP 5, PHP 7)
each — 返回数组中当前的键/值对并将数组指针向前移动一步
<?php
$foo = array("bob", "fred", "jussi", "jouni", "egon", "marliese");
$bar = each($foo);
print_r($bar);
?>
Array ( [1] => bob [value] => bob [0] => 0 [key] => 0 )
array_values
(PHP 4, PHP 5, PHP 7)
array_values — 返回数组中所有的值
array_values() 返回 input
数组中所有的值并给其建立数字索引。
array_flip
array_flip — 交换数组中的键和值
array_intersect
(PHP 4 >= 4.0.1, PHP 5, PHP 7)
array_intersect — 计算数组的交集
array_search
(PHP 4 >= 4.0.5, PHP 5, PHP 7)
array_search — 在数组中搜索给定的值,如果成功则返回首个相应的键名
array_unique
(PHP 4 >= 4.0.1, PHP 5, PHP 7)
array_unique — 移除数组中重复的值