$list = [ [ 'id' => 1, 'name' => '刘德华', 'old' => 57, 'sex' => '男' ], [ 'id' => 2, 'name' => '梁朝伟', 'old' => 55, 'sex' => '男' ], [ 'id' => 3, 'name' => '刘嘉玲', 'old' => 59, 'sex' => '女' ], [ 'id' => 4, 'name' => '俞飞鸿', 'old' => 59, 'sex' => '女' ] ]; $f_list = array_column($list, 'name', 'old');//参数:数组 新数组展示字段(仅支持1个字段) 新数组的key print_r($f_list);die; //输出结果 [ 57 => '刘德华', 55 => '梁朝伟', 59 => '俞飞鸿', ];
//array_column第二个字段传null时,显示所有字段 $list = [ [ 'id' => 1, 'name' => '刘德华', 'old' => 57, 'sex' => '男' ], [ 'id' => 2, 'name' => '梁朝伟', 'old' => 55, 'sex' => '男' ], [ 'id' => 3, 'name' => '刘嘉玲', 'old' => 59, 'sex' => '女' ], [ 'id' => 4, 'name' => '俞飞鸿', 'old' => 59, 'sex' => '女' ] ]; $f_list = array_column($list, null, 'old');//参数:数组 新数组展示字段(仅支持1个字段) 新数组的key print_r($f_list);die; //输出结果 [ 57 => [ 'id' => 1, 'name' => '刘德华', 'old' => 57, 'sex' => '男' ], 55 => [ 'id' => 2, 'name' => '梁朝伟', 'old' => 55, 'sex' => '男' ], 59 => [ 'id' => 4, 'name' => '俞飞鸿', 'old' => 59, 'sex' => '女' ] ];