• PHP array_column重新构建二维数组


    $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' => '女'
                ]
            ];
  • 相关阅读:
    使用 ES2015 编写 Gulp 构建
    ES6 Promise 接口
    Git 文件比较
    JavaScript 属性描述符
    Vim 插件之 NERDTree
    Raspberry Pi 3 Model B 安装 OSMC
    How ADB works
    [Linux] zip 与 unzip 命令
    在 Ubuntu 配置 PPTP Server
    [Linux] 查看系统启动时间
  • 原文地址:https://www.cnblogs.com/Strive-count/p/14758382.html
Copyright © 2020-2023  润新知