zoukankan      html  css  js  c++  java
  • 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' => '女'
                ]
            ];
  • 相关阅读:
    BLOB
    cesium 检测视图改变的代码
    有关Nodejs的一些插件介绍
    线程基础知识
    进程和线程概念对比
    C#对象与XMl文件之间的相互转换(转)
    简介C#读取XML的方式(转)
    关于c# Debug和Release的区别 (转)
    c++堆和栈(转)
    浅谈C#堆栈与托管堆的工作方式(转)
  • 原文地址:https://www.cnblogs.com/Strive-count/p/14758382.html
Copyright © 2011-2022 走看看