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(12345);
$b array_map("cube"$a);
print_r($b);
?>
 
Array
(
    [0] => 1
    [1] => 8
    [2] => 27
    [3] => 64
    [4] => 125
)

array_filter()没有回调函数的时候,可以过滤数组中值为空的值例如:
<?php

$entry = array(
             => 'foo',
             => false,
             => -1,
             => null,
             => ''
          );

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 " ";
}

?>
 
fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange




原文地址:https://www.cnblogs.com/agang-php/p/5056397.html