Give a chestnut:
//假设今有数组如下
$arr = [
[
'id' => '100',
'name' => 'boy',
'val' => 'handsome',
],
[
'id' => '200',
'name' => 'girl',
'val' => 'beautiful',
],
];
//期望将name提为键值, 操作如下
$re = array_column($arr, null, 'name');
print_r($re); //结果如下
[
'boy' => [
'id' => '100',
'name' => 'boy',
'val' => 'handsome',
],
'girl' => [
'id' => '200',
'name' => 'girl',
'val' => 'beautiful',
],
];
There are other applications of this function, see: https://www.php.net/manual/zh/function.array-column.php