删除php中的内部数组

时间:2016-10-21 07:12:02

标签: php arrays multidimensional-array

我必须从数组中删除内部数组。实际上,数组是通过解码JSON获得的,并且可以达到n levels。我需要根据动态密钥从其父数组中删除内部数组。下面是我尝试在php数组中引用答案的代码。

$quotationHistory = json_decode($quotationCollection->getHistory(), true);
$quotationId = 5;
foreach ($quotationHistory as $sales_id => $history) {
    foreach($history as $quotationIdValue => $values) {
        if ($quotationId == $quotationIdValue) {
            unset ($history[$quotationIdValue]);
        }
    }
}

样品:

Array
(
    [1] => Array
        (
            [5] => Array
                (
                    [0] => Array
                        (
                            [0] => 3
                            [1] => 8490.0000
                            [2] => 21-10-2016 11:43:18am
                            [3] => 24-11-2016 11:43:18am
                            [4] => 199
                            [5] => rtg
                        )

                    [1] => Array
                        (
                            [0] => 12
                            [1] => 8490.0000
                            [2] => 21-10-2016 11:43:40am
                            [3] => 24-11-2016 11:43:18am
                            [4] => 199
                            [5] => rtg
                        )

                    [2] => Array
                        (
                            [0] => 45
                            [1] => 8490.0000
                            [2] => 21-10-2016 11:43:54am
                            [3] => 24-11-2016 11:43:18am
                            [4] => 199
                            [5] => rtg
                        )

                    [3] => Array
                        (
                            [0] => 11
                            [1] => 8490.0000
                            [2] => 21-10-2016 11:44:04am
                            [3] => 24-11-2016 11:43:18am
                            [4] => 199
                            [5] => rtg
                        )

                    [4] => Array
                        (
                            [0] => 54
                            [1] => 8490.0000
                            [2] => 21-10-2016 11:44:16am
                            [3] => 24-11-2016 11:43:18am
                            [4] => 199
                            [5] => rtg
                        )

                )

        )

)

现在,我想用key = 5

删除第二级数据

2 个答案:

答案 0 :(得分:1)

你最好在原始阵列上进行取消设置:

$quotationHistory = json_decode($quotationCollection->getHistory(), true);            
foreach($quotationHistory as $sales_id => $history) {
    foreach($history as $quotationIdValue => $values) {
        if($quotationId == $quotationIdValue){
            unset($quotationHistory[$sales_id][$quotationIdValue]);
        }
    }
}

原因是内部数组作为副本传递。但您也可以通过引用指定分配:

  

为了能够直接修改循环中的数组元素,在$ value之前加上&amp ;.在这种情况下,该值将通过引用(official Php doc)分配。

$quotationHistory = json_decode($quotationCollection->getHistory(), true);            
foreach($quotationHistory as $sales_id => &$history) {
    foreach($history as $quotationIdValue => &$values) {
        if($quotationId == $quotationIdValue){
            unset($$history[$quotationIdValue]);
        }
    }
}

答案 1 :(得分:0)

您可能需要递归迭代器,因为返回可以是数组中的任何级别(可能是n levels的含义。请注意,它会删除具有相同值的任何键,因此它会删除5的任何键。最好根据值rtg来递归删除密钥:

$arr    =   array(
    'one'=>array(
        'one'=>'and a half',
        'two'=>array(
            'three'=>'three and a half'
        )
    )
);

function recurse($array,$remove=false)
    {
        foreach($array as $key => $value) {
            if($key != $remove){
                if(is_array($value)) {
                    $new[$key]  =   recurse($value,$remove);
                }
                else
                    $new[$key]  =   $value;
            }
        }

        return $new;
    }

# Run the iterator to remove the key named "three"
print_r(recurse($arr,'three'));

如果您需要按值搜索,这应该有效:

function recurseValue($array,$remove=false)
    {
        foreach($array as $key => $value) {
            if(is_array($value)) {
                $new[$key]  =   recurseValue($value,$remove);
            }
            else {
                if($value != $remove){
                    $new[$key]  =   $value;
                }
            }
        }

        return $new;
    }
# Run the iterator to remove the key with the value named "Three and a half"
print_r(recurseValue($arr,'three and a half'));

给你:

Array
(
    [one] => Array
        (
            [one] => and a half
            [two] => 
        )

)

这最后一个选项会将数组规范化为一个级别,因此您可以正常循环它:

function recurseArray($array,&$new,$bkey = false)
    {
        foreach($array as $key => $value) {
            if(is_array($value)) {
                recurseArray($value,$new,$key);
            }
            else {
                $new[$bkey][]   =   $value;
            }
        }
    }

$new = array();
recurseArray($arr,$new);
print_r($new);

给你:

Array
(
    [0] => Array
        (
            [0] => 3
            [1] => 8490.0000
            [2] => 21-10-2016 11:43:18am
            [3] => 24-11-2016 11:43:18am
            [4] => 199
            [5] => rtg
        )

    [1] => Array
        (
            [0] => 3
            [1] => 8490.0000
            [2] => 21-10-2016 11:43:18am
            [3] => 24-11-2016 11:43:18am
            [4] => 199
            [5] => rtg
        )

    [2] => Array
        (
            [0] => 3
            [1] => 8490.0000
            [2] => 21-10-2016 11:43:18am
            [3] => 24-11-2016 11:43:18am
            [4] => 199
            [5] => rtg
        )

    [3] => Array
        (
            [0] => 3
            [1] => 8490.0000
            [2] => 21-10-2016 11:43:18am
            [3] => 24-11-2016 11:43:18am
            [4] => 199
            [5] => rtg
        )
)