1.array_map
array array_map(callable $callback, array $arr1 [, array $...])
array_map()返回一个数组,该数组包含了arr1中的所有单元经过callback作用之后的单元。callback接受的参数数目应该和传递给array_map()函数的数组数目一致。
例一:
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 show_Spanish($n, $m) {
return("The number $n is called $m in Spanish");
}
$a = array(1, 2, 3, 4, 5);
$b = array("uno", "dos", "tres", "cuatro", "cinco");
$c = array_map("show_Spanish", $a, $b);
print_r($c);
输出:
Array ( [0] => The number 1 is called uno in Spanish [1] => The number 2 is called dos in Spanish [2] => The number 3 is called tres in Spanish [3] => The number 4 is called cuatro in Spanish [4] => The number 5 is called cinco in Spanish )
例三:$a = array(1, 2, 3, 4, 5);
$b = array("one", "two", "three", "four", "five");
$c = array("uno", "dos", "tres", "cuatro", "cinco");
$d = array_map(null, $a, $b, $c);
print_r($d);
输出:
Array ( [0] => Array ( [0] => 1 [1] => one [2] => uno ) [1] => Array ( [0] => 2 [1] => two [2] => dos ) [2] => Array ( [0] => 3 [1] => three [2] => tres ) [3] => Array ( [0] => 4 [1] => four [2] => cuatro ) [4] => Array ( [0] => 5 [1] => five [2] => cinco ) )
注:通常使用了两个或更多数组时,它们的长度应该相同,因为回调函数是平行作用于相应的单元上的。如果数组的长度不同,则最短的一个将被用空的单元扩充。
2.array_unshift
int array_unshift(array $array, mixed $var [,mixed $...])
array_unshift()将传入的单元插入到array数组的开头。注意单元是作为整体被插入的,因此传入单元将保持同样的顺序。所有的数值键名将修改为从零开始重新计数,所有的文字键名保持不变。
参数说明:
array-------输入的数组
var----------开头插入的变量。
返回值:返回array数组新的单元数目。
例子:
$queue = array("orange", "banana");
array_unshift($queue, "apple", "raspberry");
print_r($queue);
输出:
Array ( [0] => apple [1] => raspberry [2] => orange [3] => banana )
3.array_shift
mixed array_shift(array $array)
array_shift()将array的第一个单元移出并作为结果返回,将array的长度减一并将所有其它单元向前移动一位。所有的数字键名将改为从零开始计数,文字键名将不变。
Note:使用此函数后会重置(reset)array指针。
参数说明:
array-----输入的数组。
返回值:返回移出的值,如果array为空或不是一个数组则返回NULL.
例子:
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_shift($stack);
print_r($stack);
输出:
Array ( [0] => banana [1] => apple [2] => raspberry )