不灭的焱

革命尚未成功,同志仍须努力

作者:php-note.com  发布于:2019-06-01 10:38  分类:PHP基础 

一般array_column 是获取二维数组的制定列,组成新的数组输出。

注意:可以制定第三个参数,指定输出数组的key

<?php
$arr = [
    ['id' => 11, 'name' => 'aa'],
    ['id' => 22, 'name' => 'bb',],
    ['id' => 44, 'name' => 'cc'],
    ['id' => 33, 'name' => 'dd']
];

print_r(array_column($arr, 'name')); 
// 运行结果:
Array
(
    [0] => aa
    [1] => bb
    [2] => cc
    [3] => dd
)

print_r(array_column($arr,'name','id')); 
// 运行结果:
(
    [11] => aa
    [22] => bb
    [44] => cc
    [33] => dd
)

print_r(array_column($arr, null, 'name')); 
// 运行结果:
Array
(
    [aa] => Array
        (
            [id] => 11
            [name] => aa
        )

    [bb] => Array
        (
            [id] => 22
            [name] => bb
        )

    [cc] => Array
        (
            [id] => 44
            [name] => cc
        )

    [dd] => Array
        (
            [id] => 33
            [name] => dd
        )

)