dryeyhe0266 2016-12-12 03:00
浏览 36
已采纳

如何在PHP中将多维数组合并到单维数组中

I want to merges all the values from identification,general,address,employer array to the single line of $aa array as below format.

the original array to merges.

  $aa = [
        0 => [
            'identification' => [
                1 => ['Iden_type' => 'types'],
                2 => ['Iden_num' => '000215'],
            ],
            'general' => [
                1 => ['gen_name' => 'name'],
                2 => ['gen_lname' => 'lname'],
            ],
            'address' => [
                1 => ['add_type' => 'type'],
                2 => ['add_text' => 'text'],
            ],
            'contact' => [
                1 => ['cont_type' => 'types'],
                2 => ['cont_text' => 'text'],
            ],
            'employer' => [
                1 => ['emp_fname' => 'first name'],
                2 => ['emp_lname' => 'last name'],
            ],

        ]
    ];

Result that I want to get from above array.

 $aa = [
        0 => [
            'types',
            '000215',
            'name',
            'lname',
            'type',
            'text',
            'types',
            'text',
            'first name',
            'last name',

        ],
        1 => [
            'types',
            '000215',
            'name',
            'lname',
            'type',
            'text',
            'types',
            'text',
            'first name',
            'last name',

        ],

    ];
  • 写回答

1条回答 默认 最新

  • 「已注销」 2016-12-12 03:35
    关注
    foreach ($aa as $k => $v) {
        $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($v));
        foreach ($it as $value) {
            $result[$k][] = $value;
        }
    }
    

    SEE WORKING DEMO

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址