array_column()用法

<?php

$arr = [
    [
        'id' => 1,
        'name' => 'a'
    ],[
        'id' => 2,
        'name' => 'b',
    ], [
        'id' => 4,
        'name' => 'c'
    ], [
        'id' => 3,
        'name' => 'd'
    ]
];

print_r(array_column($arr,'name'));

//运行结果:Array ( [0] => a [1] => b [2] => c [3] => d )

print_r(array_column($arr,'name','id'));
//运行结果:Array ( [1] => a [2] => b [4] => c [3] => d )

print_r(array_column($arr, null, 'name'));
//运行结果:Array ( [a] => Array ( [id] => 1 [name] => a ) [b] => Array ( [id] => 2 [name] => b ) [c] => Array ( [id] => 4 [name] => c ) [d] => Array ( [id] => 3 [name] => d ) )

 

posted @ 2019-11-05 10:31  戈丫汝  阅读(2029)  评论(2编辑  收藏  举报