删除或合并多维数组

时间:2019-04-01 09:53:20

标签: php arrays multidimensional-array

如何从这样的数组

[0] => Array
    (
        [0] => Array
            (
                [0] => Array
                    (
                        [SKU] => T
                        [title] => BT
                        [min] => SPads
                    )

                [1] =>
            )
        [1] => Array
            (
                [0] => Array
                    (
                        [SKU] => R
                        [title] => RT
                        [min] => XPads
                    )

                [1] =>
            )
    )

创建如下数组:

        [0] => Array
            (
                [0] => Array
                    (
                        [SKU] => T
                        [title] => BT
                        [min] => SPads
                    )

                [1] =>
            )
        [1] => Array
            (
                [0] => Array
                    (
                        [SKU] => R
                        [title] => RT
                        [min] => XPads
                    )

                [1] =>
            )

以及如何像这样排列

[0] => Array
    (
        [0] => Array
            (
                [0] => Array
                    (
                        [SKU] => T
                        [title] => BT
                        [min] => SPads
                    )

                [1] =>
            )

    )

像这样制作数组:

[0] => Array
    (

                [0] => Array
                    (
                        [SKU] => T
                        [title] => BT
                        [min] => SPads
                    )

                [1] =>

    )

我只发现了用值删除多维数组的灵魂,但是这里我没有价值,我只有键,有帮助吗?

array_splice和array_diff是我用来从此网站https://www.php.net解决我的问题的php函数,但是我没有找到解决方案。

2 个答案:

答案 0 :(得分:2)

使用array_shift可以删除第一个索引,请参见下面的演示。

<?php
//Enter your code here, enjoy!
$a = [
    0 => [
        0 => [
            0 => [
                'SKU'   => 'T',
                'title' => 'BT',
                'min'   => 'SPads',
            ],
            1 => '',
        ],
        [
            0 => [
                'SKU'   => 'R',
                'title' => 'RT',
                'min'   => 'XPads',
            ],
            1 => '',
        ],
    ],
];
$a = array_shift($a);
print_r($a);

array_shift-将元素移出数组的开头

输出

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [SKU] => T
                    [title] => BT
                    [min] => SPads
                )

            [1] => 
        )

    [1] => Array
        (
            [0] => Array
                (
                    [SKU] => R
                    [title] => RT
                    [min] => XPads
                )

            [1] => 
        )

)

Demo

答案 1 :(得分:0)

您可以在foreach循环中进行迭代,并在所需的formate中创建新的数组。

$newAr =array();

foreach ($oldArr as $tempArr){
 foreach($tempArr as $tempArr2){
   $newAr[] = $tempArr2;
 }
}

print_r($newArr);