1. 程式人生 > >PHP array_column用法

PHP array_column用法

一般array_column是獲取二維陣列的制定列,組成新的陣列輸出。今天看到有可以制定第三個引數,制定輸出陣列的key。

<?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 ) )