1

I have try to remove whole parent array using PHP unset(key) function based on value from a nested array in it.

Nested array looks like

Array ( 
[0] => Array ( 
    [ID] => 9909 
    [SHIPPING_AMOUNT] => 5 
    [TOTAL_TAX] => 0 
    [GRAND_AMOUNT] => 49.97 
    [ITEMS_AMOUNT] => 44.97 
    [ITEMS] => Array ( 
        [0] => Array ( 
            [CODE] => TEST
            [QTY] => 1 
            [UNIT_PRICE] => 14.99 ) 
        [1] => Array ( 
            [CODE] => NNKIT 
            [QTY] => 1 
            [UNIT_PRICE] => 9.99 ) 
        [2] => Array ( 
            [CODE] => MAINKIT 
            [QTY] => 1 
            [UNIT_PRICE] => 19.99 )
        ) 
    ) 
[1] => Array ( 
    [ID] => 9910 
    [SHIPPING_AMOUNT] => 5 
    [TOTAL_TAX] => 0 
    [GRAND_AMOUNT] => 74.96 
    [ITEMS_AMOUNT] => 69.96 
    [ITEMS] => Array ( 
        [0] => Array ( 
            [CODE] => NNKIT 
            [QTY] => 1 
            [UNIT_PRICE] => 9.99 ) 
        [1] => Array ( 
            [CODE] => MAINKIT 
            [QTY] => 3 
            [UNIT_PRICE] => 19.99 ) 
        ) 
    ) 
[2] => Array ( 
    [ID] => 9911 
    [SHIPPING_AMOUNT] => 5 
    [TOTAL_TAX] => 0 
    [GRAND_AMOUNT] => 44.98 
    [ITEMS_AMOUNT] => 39.98 
    [ITEMS] => Array ( 
        [0] => Array ( 
            [CODE] => MAINKIT 
            [QTY] => 2 
            [UNIT_PRICE] => 19.99 ) 
        ) 
    ) 
[3] => Array ( 
    [ID] => 9912 
    [SHIPPING_AMOUNT] => 5 
    [TOTAL_TAX] => 0 
    [GRAND_AMOUNT] => 29.98 
    [ITEMS_AMOUNT] => 24.98 
    [ITEMS] => Array ( 
        [0] => Array ( 
            [CODE] => TEST
            [QTY] => 1 
            [UNIT_PRICE] => 14.99 ) 
        [1] => Array ( 
            [CODE] => NEWTEST
            [QTY] => 1 
            [UNIT_PRICE] => 9.99 )
        )
    ) 

and value which I check for is CODE = MAINKIT. If not exists in a nested array then main array should be removed (in this case parent array[3]) but somehow returned key is from nested array not a parent one.

PHP code:

foreach($array as $key => $value){
    if(is_array($value) && $value['CODE'] != 'MAINKIT')
          unset($key);
}
0

1 Answer 1

3

The CODE elements are inside the $value['ITEMS'] array, not directly in $value.

To tell whether MAINKIT isn't in any of the items, you have to loop through all the items, testing whether any of them matches. If none do, then delete the array element.

Try:

foreach($array as $key => $value){
    if (is_array($value) && is_array($value['ITEMS']))
        $found_mainkit = false;
        foreach ($value['ITEMS'] as $item) {
            if($item['CODE'] == 'MAINKIT') {
                $found_mainkit = true;
                break;
            }
        }
        if (!$found_mainkit) {
            unset($array[$key]);
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Looks like is doing similar to mine, just one step further - deleting nested ITEMS array not the whole parent array
It's deleting $array[$key], not $item.
I see the problem, give me a sec.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.