-1

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.

4
  • 3
    Possible duplicate of How can I remove a key and its value from an associative array? Commented Nov 8, 2017 at 15:17
  • @MohammadTomaraei: probably for you these two are the same, but for me as beginner they're quite different! Commented Nov 8, 2017 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. Commented Nov 8, 2017 at 20:25
  • @MohammadTomaraei: Just because it's too much easy for a smart person like you, should it be down voted!? Commented Nov 8, 2017 at 20:27

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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']);
    }        
?>

Comments

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.