使用PHP将3D阵列转换为2D阵列

时间:2015-01-30 14:05:45

标签: php arrays

我的表单生成一个三维数组。我想将这个数组转换成二维数组。

我试过这个没有成功:

$_rows = array();
foreach ($_contacts as $name => $_arr) {
   foreach ($_arr as $key => $val) {
        $_rows[] = array ($name => $val);
   }
}

数据来源:

[_contacts] => Array
    (

    [name] => Array
            (
                [0] => foo
                [1] => bar
            )

    [phone] => Array
            (
                [0] => 012345
                [1] => 098765
            )

    [email] => Array
            (
                [0] => mail.com
                [1] => yahoo.com
            )
    )

期望的输出:

 Array
    (
        [0] => Array
            (
                [name] => foo
                [phone] => 012345
                [email] => mail.com
            )

        [1] => Array
            (
                [name] => bar
                [phone] => 098765
                [email] => yahoo.com
            )
    )

任何想法都是我错了?

1 个答案:

答案 0 :(得分:2)

$_rows = array();     
foreach ($_contacts as $name => $_arr) {          
   foreach ($_arr as $key => $val) {            
        $_rows[$key][$name] = $val;
   }
}