返回數組中指定的一列,將鍵值作為元素鍵名array_column
阿新 • • 發佈:2019-04-12
ber arr erb 輸出 集中 jobs 表示 color null
array_column() 函數
從記錄集中取出 last_name 列:
<?php // 表示由數據庫返回的可能記錄集的數組 $a = array( array( ‘id‘ => 5698, ‘first_name‘ => ‘Bill‘, ‘last_name‘ => ‘Gates‘, ), array( ‘id‘ => 4767, ‘first_name‘ => ‘Steve‘, ‘last_name‘ => ‘Jobs‘, ), array( ‘id‘ => 3809, ‘first_name‘ => ‘Mark‘, ‘last_name‘ => ‘Zuckerberg‘, ) ); $last_names = array_column($a, ‘last_name‘); print_r($last_names); ?>
輸出“”
Array ( [0] => Gates [1] => Jobs [2] => Zuckerberg )
從記錄集中取出 last_name 列,用相應的 "id" 列作為鍵值:
<?php // 表示由數據庫返回的可能記錄集的數組 $a = array( array( ‘id‘ => 5698, ‘first_name‘ => ‘Bill‘, ‘last_name‘ => ‘Gates‘, ), array( ‘id‘ => 4767, ‘first_name‘ => ‘Steve‘, ‘last_name‘ => ‘Jobs‘, ) array( ‘id‘ => 3809, ‘first_name‘ => ‘Mark‘, ‘last_name‘ => ‘Zuckerberg‘, ) ); $last_names =array_column($a, ‘last_name‘, ‘id‘)
; print_r($last_names); ?>
輸出:
Array ( [5698] => Gates [4767] => Jobs [3809] => Zuckerberg )
註意:
不返回莫一列,直接將全部數組返回,然後將元素值作為鍵名,中間字段為空
$newArray1 = array_column($data,NULL,‘uid‘);
返回數組中指定的一列,將鍵值作為元素鍵名array_column