简体   繁体   中英

How to remove elements where specific values are repeating in array in php?

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.

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;
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM