array_column() 返回输入数组中某个单一列的值。
array_column(array,column_key,index_key);
array and column_key 必填,index_key 可选用作返回数组的索引/键的列
用例1:
$product = [
['product_id'=>262145, 'product_name'=>'中信','base_id'=>282],
['product_id'=>262146, 'product_name'=>'长城','base_id'=>122],
['product_id'=>262147, 'product_name'=>'万科','base_id'=>111]
];
$productId = array_column($product,'product_id');
var_dump($productId);
输出:
array(3) {
[0]=>
int(262145)
[1]=>
int(262146)
[2]=>
int(262147)
}
用例2:以base_id 做索引的键
$productIds = array_column($product,'product_id','base_id');
var_dump($productIds);
输出:
array(3) {
[282]=>
int(262145)
[122]=>
int(262146)
[111]=>
int(262147)
}