三维数组转二维数组

泄露秘密 提交于 2020-03-18 13:42:12
//示例三维数组
$bom = Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [carrier] => 2
                )

            [1] => Array
                (
                    [name] => B1(硝酸)
                )

            [2] => Array
                (
                    [material_id] => 6
                )

            [3] => Array
                (
                    [content] => 1
                )

            [4] => Array
                (
                    [content_unit] => g
                )

            [5] => Array
                (
                    [dosage] => 1.087
                )

            [6] => Array
                (
                    [dosage_unit] => kg
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [carrier] => 2
                )

            [1] => Array
                (
                    [name] => K3(MNB)43%
                )

            [2] => Array
                (
                    [material_id] => 8
                )

            [3] => Array
                (
                    [content] => 2
                )

            [4] => Array
                (
                    [content_unit] => g
                )

            [5] => Array
                (
                    [dosage] => 4.651
                )

            [6] => Array
                (
                    [dosage_unit] => kg
                )

        )
)
//转为二维数组
function array3_to_array2($bom)
    {
        $b = array();
        foreach ($data as $v) {
            $c = array();
            foreach ($v as $vv) {
                $key = array_keys($vv); //获取数组的键
                $value = array_values($vv); //获取数组的值
                $c[$key[0]] = $value[0]; //拼接
            }
            $b[] = $c;
        }
        return $b;
    }

//返回的二维数组

Array
(
    [0] => Array
        (
            [carrier] => 2
            [name] => B1(硝酸)
            [material_id] => 6
            [content] => 1
            [content_unit] => g
            [dosage] => 1.087
            [dosage_unit] => kg
        )

    [1] => Array
        (
            [carrier] => 2
            [name] => K3(MNB)43%
            [material_id] => 8
            [content] => 2
            [content_unit] => g
            [dosage] => 4.651
            [dosage_unit] => kg
        )
)

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!