0

I have a complicated nested array which I want to remove all items and their children with specific store_id:
It's really difficult and I don't know how to figure it out.

Array
(
[0] => Array
    (
        [cart_id] => 89
        [product_id] => 46
        [store_id] => 2
        [option] => Array
            (
                [0] => Array
                    (
                        [product_option_id] => 92
                        [value] => Aqua
                    )

                [1] => Array
                    (
                        [product_option_id] => 91
                        [value] => 85C
                    )
            )
    )

[1] => Array
    (
        [cart_id] => 90
        [product_id] => 46
        [store_id] => 2
    )

Many thanks for any kind help.

Kardo
  • 1,658
  • 4
  • 32
  • 52
  • 3
    Possible duplicate of [How can I remove a key and its value from an associative array?](https://stackoverflow.com/questions/3053517/how-can-i-remove-a-key-and-its-value-from-an-associative-array) – Mohammad Tomaraei Nov 08 '17 at 15:17
  • @MohammadTomaraei: probably for you these two are the same, but for me as beginner they're quite different! – Kardo Nov 08 '17 at 20:24
  • @MohammadTomaraei: I don't understand the down vote. It seems like a reasonable question to ask. For me it's a question and I didn't find any proper answer in google matching with my problem, so what am I missing that makes this question get down votes? Not picking a fight, just curious. Peace. – Kardo Nov 08 '17 at 20:25
  • @MohammadTomaraei: Just because it's too much easy for a smart person like you, should it be down voted!? – Kardo Nov 08 '17 at 20:27

2 Answers2

5

If you want to remove the entire array element if it has a specific store_id, you just need to loop over the array, check the store_id and remove the element if you don't want it anymore.

E.g.:

<?php         
    foreach($data as $key=>$row){
        if($row['store_id'] == 2){
          unset($data[$key]);
        }
    }        
?>

You can change that '2' to be anything you want to specifically remove a store. Or you could change the if to match an array of ids if you want to match several stores.

mouckatron
  • 1,289
  • 2
  • 13
  • 23
1

Here is the example for unsetting the cart_id in multi dimensional array.

<?php         
    foreach($data as $key=>$row){
          unset($data[$key]['cart_id']);
    }        
?>
Faiz Rasool
  • 1,379
  • 1
  • 12
  • 20