PHP二维数组用某个字段的值当做键名

$array = array(
    array('id'=>'3','name'=>'tom'),
    array('id'=>'5','name'=>'jerry'),
    array('id'=>'24','name'=>'spike')
);
 
$newArray1 = array_column($array,NULL,'id');
var_dump($newArray1);
//结果
array(
    '3'=>array('id'=>'3','name'=>'tom'),
    '5'=>array('id'=>'5','name'=>'jerry'),
    '24'=>array('id'=>'24','name'=>'spike')
)
 
$newArray2 = array_column($array,NULL,'name');
var_dump($newArray2);
//结果
array(
    'tom'=>array('id'=>'3','name'=>'tom'),
    'jerry'=>array('id'=>'5','name'=>'jerry'),
    'spike'=>array('id'=>'24','name'=>'spike')
)

 

posted @ 2019-09-03 09:24  御世制人  阅读(1970)  评论(0编辑  收藏  举报