php array_map array_filter sort
array_map — Applies the callback to the elements of the given arrays (处理映射)
array_filter — Filters elements of an array using a callback function (过滤)
<?php
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 )
array_filter()没有回调函数的时候,可以过滤数组中值为空的值例如:
<?php
$entry = array(
0 => 'foo',
1 => false,
2 => -1,
3 => null,
4 => ''
);
print_r(array_filter($entry));
?>
Array ( [0] => foo [2] => -1 )
sort This function sorts an array. Elements will be arranged from lowest to highest when this function has completed.
<?php
$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
echo "fruits[" . $key . "] = " . $val . "\n";
}
?>
fruits[0] = apple fruits[1] = banana fruits[2] = lemon fruits[3] = orange