1

I have array like this:

Array
(
    [0] => stdClass Object
        (
            [model_id] => 1
            [category_id] => 1
            [model_name] => Xperia
            [category_name] => Mobile
            [category_image] => 81649e37620644bec47244fda5233363.jpg
            [item_type] => 1
        )

    [1] => stdClass Object
        (
            [model_id] => 3
            [category_id] => 1
            [model_name] => HTC One
            [category_name] => Mobile
            [category_image] => 250a3e4655454feaec8e2537d149846a.jpg
            [item_type] => 1
        )

    [2] => stdClass Object
        (
            [model_id] => 9
            [category_id] => 3
            [model_name] => Apple TV
            [category_name] => Televisions
            [category_image] => no_image.jpg
            [item_type] => 1
        )

    [3] => stdClass Object
        (
            [model_id] => 4
            [category_id] => 2
            [model_name] => MacBook
            [category_name] => Laptop
            [category_image] => c9890535d26bd06189fc22940a5ee5da.jpg
            [item_type] => 1
        )

    [4] => stdClass Object
        (
            [model_id] => 2
            [category_id] => 1
            [model_name] => iPhone 7
            [category_name] => Mobile
            [category_image] => cab0631b071d0562412ccab2279d391e.jpg
            [item_type] => 1
        )

    [5] => stdClass Object
        (
            [model_id] => 5
            [category_id] => 3
            [model_name] => Samsung TV
            [category_name] => Televisions
            [category_image] => e60bd4822e516a93f1e99a8b456b70ce.jpg
            [item_type] => 1
        )
)

How do I remove elements where category_id as well as item_type are repeated ? With the code below, I could only remove elements where any single value is repeating.

foreach($all_device_categories as $arr){
    if(!isset($result[$arr->category_id])){
        $result[$arr->category_id] = $arr;
    }
}

Update: category_id as well as item_type should not be repeated at the same time. Its fine if category_id repeats but not item_type or vice versa.

7
  • What you really want? For both repeating? or individual catgory_id or item_type? Commented Jun 29, 2017 at 11:40
  • So yu want to match both keys?category_id & item_type? Commented Jun 29, 2017 at 11:41
  • @FrayneKonok see my update. Commented Jun 29, 2017 at 11:44
  • @RabinLama, Checked. But the confusion is if a category_id repeat but not the item_type then what to do?? Did you mean both the category_id and item_type repeat at a same time?? Commented Jun 29, 2017 at 11:46
  • @FrayneKonok See my update again. Commented Jun 29, 2017 at 11:53

2 Answers 2

2

As you need to store only one value if both category_id and item_type repeats then you need to concat them using some other characters like: -. If you don't do it then some information will be missed out.

foreach($all_device_categories as $arr){
    $result[$arr->category_id.'-'.$arr->item_type] = $arr;        
}

I think this will help you, This will always takes the update array values if any match found. And if you don't want to take the updates value then simply you need to apply a condition before storing new array.

foreach($all_device_categories as $arr){
    if(!isset($result[$arr->category_id.'-'.$arr->item_type])){
        $result[$arr->category_id.'-'.$arr->item_type] = $arr;
    }
}

The second example will store only the first match, if the repeats comes then it will avoid.

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

1 Comment

Works nice. Thank you for your time and effort.
2

For multiple key match. You can combine those keys into array key. Try this:

foreach($all_device_categories as $arr){
    if(!isset($result[$arr->category_id.$arr->item_type])){
        $result[$arr->category_id.$arr->item_type] = $arr;
    }
}

1 Comment

You answer has a bug, suppose if category_id is 31 and item_type is 1 then it will store first time, and again say category_id is 3 and item_type is 11 then what will happen in your current solution??

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.