三维数组转二维数组

Posted xiangangxu1997

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了三维数组转二维数组相关的知识,希望对你有一定的参考价值。

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

 

以上是关于三维数组转二维数组的主要内容,如果未能解决你的问题,请参考以下文章

php数组三维转二维

C基础数组和字符串的使用

C基础数组和字符串的使用

将数组进行转置

matlab中如何计算二维数组大小?

c++怎么用三维数组