Is it possible to assign keys to array elements in PHP from a value column with less code?

前端 未结 3 1123
日久生厌
日久生厌 2021-01-20 00:16

Let\'s assume I have an array of elements, which are arrays themselves, like so:

$array = [
    [\'foo\' => \'ABC\', \'bar\' => \'DEF\'],
    [\'foo\'          


        
3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-20 00:38

    You can also do it with array_reduce

    $new_array = array_reduce($array, function($carry, $item) {
        $carry[$item['foo']] = $item;
        return $carry;
    }, []);
    

提交回复
热议问题