How can I create an array from the values of another array's key?

前端 未结 4 759
走了就别回头了
走了就别回头了 2021-02-01 18:38

I have an array as follows:

$arr1 = array(
  0 => array(
    \'name\' => \'tom\',
    \'age\' => 22
  ),
  1 => array(
    \'name\' => \'nick\',
           


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-01 19:36

    This can be done in still more simple way by using array_column

    $arr2= array_column($arr1, 'name');
    
    print_r($arr2); //Array ( [0] => tom [1] => nick )
    

    array_column is used to get the columns of a sub-array.

提交回复
热议问题