按值从多维数组中删除子数组

时间:2016-03-10 02:28:17

标签: php multidimensional-array

我有这个API,它提供了一个如下所示的数组:

[Air Strike] => Array
    (
        [defindex] => Array
            (
                [0] => 1104
            )

        [prices] => Array
            (
                [6] => Array
                    (
                        [Tradable] => Array
                            (
                                [Craftable] => Array
                                    (
                                        [0] => Array
                                            (
                                                [currency] => metal
                                                [value] => 0.05
                                                [last_update] => 1415072318
                                                [difference] => -0.06
                                                [value_raw] => 0.05
                                            )

                                    )

                                [Non-Craftable] => Array
                                    (
                                        [0] => Array
                                            (
                                                [currency] => metal
                                                [value] => 0.11
                                                [last_update] => 1436478504
                                                [difference] => 0.03
                                                [value_raw] => 0.11
                                            )

                                    )

                            )

                    )

                [11] => Array
                    (
                        [Tradable] => Array
                            (
                                [Craftable] => Array
                                    (
                                        [0] => Array
                                            (
                                                [currency] => metal
                                                [value] => 4
                                                [last_update] => 1456277271
                                                [difference] => -0.165
                                                [value_raw] => 4
                                            )

                                    )

                                [Non-Craftable] => Array
                                    (
                                        [0] => Array
                                            (
                                                [currency] => metal
                                                [value] => 3.66
                                                [last_update] => 1456277553
                                                [difference] => -0.34
                                                [value_raw] => 3.66
                                            )                  

                                    )

                            )

                    )

            )

    )

有没有什么方法可以删除子数组,因为如果项目无法解决或者[' value_raw']小于3,它们会因为缺少更好的单词而分歧?

我想要的输出是这样的:

[Air Strike] => Array
(
    [defindex] => Array
        (
            [0] => 1104
        )

    [prices] => Array
        (
           [11] => Array
                (
                    [Tradable] => Array
                        (
                            [Craftable] => Array
                                (
                                    [0] => Array
                                        (
                                            [currency] => metal
                                            [value] => 4
                                            [last_update] => 1456277271
                                            [difference] => -0.165
                                            [value_raw] => 4
                                        )

                                )

                            [Non-Craftable] => Array
                                (
                                    [0] => Array
                                        (
                                            [currency] => metal
                                            [value] => 3.66
                                            [last_update] => 1456277553
                                            [difference] => -0.34
                                            [value_raw] => 3.66
                                        )                  

                                )

                        )

                )

        )

)

我对此的看法是使用嵌套的foreach()循环迭代每个子数组并检查[' value_raw']的值以及它是否可以交易,但是如何我会知道要移除多少个子阵列而不会移除太多?也许从priceindex开始倒退,然后检查它是否包含多个子数组,以及它是否与其所有内容一起删除它?

1 个答案:

答案 0 :(得分:0)

您可以通过一系列嵌套的foreach()循环获得所需的结果:

foreach( $airStrike['prices'] as $gKey => $group )
{
    foreach( $group as $sKey => $subGroup )
    {
        if( $sKey !== 'Tradable' ) unset( $airStrike['prices'][$gKey][$sKey] );
        else
        {
            foreach( $subGroup as $iKey => $item )
            {
                if( $item[0]['value_raw'] < 3 ) unset( $airStrike['prices'][$gKey][$sKey][$iKey] );
            }

            if( !count( $airStrike['prices'][$gKey][$sKey] ) ) unset( $airStrike['prices'][$gKey][$sKey] );
        }
        if( !count( $airStrike['prices'][$gKey] ) ) unset( $airStrike['prices'][$gKey] );
    }
}

3v4l demo

未设置键≠'Tradable'的项目。处理'可交易'子项,如果'value_raw'是&lt; 3,未设置。参考您的示例,'Craftable'和'Non-Craftable'子数组被认为只有一个子数组:如果它们可以有多个子数组,则必须添加一个额外的{{1 } iside foreach()

请注意,在level-2和level-3 foreach( $subGroup )的末尾,我检查当前数组是否为空,如果是 - 我取消设置。