码迷,mamicode.com
首页 > 编程语言 > 详细

三维数组转二维数组

时间:2020-03-18 13:40:41      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:style   bsp   UNC   foreach   pre   value   each   二维数组   unit   

//示例三维数组
$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
        )
)

 

三维数组转二维数组

标签:style   bsp   UNC   foreach   pre   value   each   二维数组   unit   

原文地址:https://www.cnblogs.com/xiangangXu1997/p/12516725.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!