PHP 常用到的 Array 函数

2018-07-13  本文已影响8人  冰镇苏打水

PHP 常用到的 Array 函数

array_column 返回输入数组中某个单一列的值

$arr = array(
    array(
        'id' => 111,
        'first_name' => 'zhang',
        'last_name' => 'san'
    ),
    array(
        'id' => 112,
        'first_name' => 'li',
        'last_name' => 'si'
    ),
    array(
        'id' => 113,
        'first_name' => 'wang',
        'last_name' => 'wu'
    )
);

$last_names = array_column($arr, 'last_name', 'id');
print_r($last_names);

输出:

Array
(
    [111] => san
    [112] => si
    [113] => wu
)

array_filter 用回调函数过滤数组中的元素

function test_odd($var){
    return(gettype($var) === 'integer');
}

$arr = array('a','b',2,3,4);

print_r(array_filter($arr,"test_odd"));

输出:

Array
(
    [2] => 2
    [3] => 3
    [4] => 4
)

array_map 将用户自定义函数作用到给定数组的每个值上

此函数与array_walk_recursive类似,需要写递归

function myFun($val){
    if(is_array($val)){
        return array_map('myFun',$val);
    }
    return($val*$val);
}

$arr = array(1,2,3,4,5,6 => [2,3]);
print_r(array_map('myFun',$arr));

输出:

Array
(
    [0] => 1
    [1] => 4
    [2] => 9
    [3] => 16
    [4] => 25
    [6] => Array
        (
            [0] => 4
            [1] => 9
        )

)

------------------------------

function myFun($val1,$val2){
    $result = 'different';
    if($val1 === $val2){
        $result = 'same';
    }
    return $result;
}

$arr1 = array('Mixj','Binz','Tiec');
$arr2 = array('Mixj','BinZ','Tiec');

print_r(array_map('myFun',$arr1,$arr2));

输出:

Array
(
    [0] => same
    [1] => different
    [2] => same
)

array_walk_recursive 对数组中的每个成员递归地应用用户函数

此函数与 array_map 类似,省去了写递归

function myfunction($value,$key)
{
echo "The key $key has the value $value<br>";
}
$a1=array("a"=>"red","b"=>"green");
$a2=array($a1,"1"=>"blue","2"=>"yellow");
array_walk_recursive($a2,"myfunction");

输出:

The key a has the value red
The key b has the value green
The key 1 has the value blue
The key 2 has the value yellow

extract / compact

extract 从数组中将变量导入到当前的符号表
compact 创建一个包含变量名和它们的值的数组

$arr = array(
    "a" => "Cat",
    "b" => "Dog",
    "c" => "Horse"
);
extract($arr);
echo "\$a = $a; \$b = $b; \$c = $c";

输出:

$a = Cat; $b = Dog; $c = Horse

$firstName = "Mi";
$lastName = "xj";
$age = 24;
$result = compact("firstName","lastName","age");
print_r($result);

输出:

Array
(
    [firstName] => Mi
    [lastName] => xj
    [age] => 24
)

uasort 使用自定义的比较函数对键值进行排序 (可以对二维数组排序)

$arr = [
    [
        'id' => 3,
        'age' => 33
    ],
    [
        'id' => 2,
        'age' => 44
    ],
    [
        'id' => 1,
        'age' =22
    ]
];

uasort($arr,function($a, $b){
    $field = 'age';
    if($a[$field] == $b[$field]){
        return 0;
    }
    return $a[$field] < $b[$field] ? -1 : 1;
});

print_r($arr);

输出:

Array
(
    [2] => Array
        (
            [id] => 1
            [age] => 22
        )

    [0] => Array
        (
            [id] => 3
            [age] => 33
        )

    [1] => Array
        (
            [id] => 2
            [age] => 44
        )
)
上一篇下一篇

猜你喜欢

热点阅读