PHP 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 ) )
点赞

发表评论