Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Eliminate duplicate rows in stdclass in php

Posted on 2013-02-06
5
Medium Priority
?
486 Views
Last Modified: 2013-02-06
hi experts,

i am new to php, can any guide me how to eliminate duplicate rows in stdclass based on key value.
this is my array, i wants to eliminate duplicate entries based on id. clear me based on this code.
Array (
            [0] => stdClass Object
                        (                         
                        [itemname] => Airindia                         
                        [id] => 9                         
                        )
            [1] => stdClass Object
                        (                         
                        [itemname] => honda city                         
                        [id] => 1                         
                        )
            [2] => stdClass Object
                        (                         
                        [itemname] => honda city                        
                        [id] => 1                         
                        )
            [3] => stdClass Object
                        (                         
                        [itemname] => hero honda                         
                        [id] => 2                         
                        )
            )
0
Comment
Question by:codeoxygen
  • 2
  • 2
5 Comments
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 38859547
foreach($your_array as $obj){
    $hash = $element["itemname"]
    $unique_array[$hash] = $obj
}

Open in new window

or
$uniques = array();
foreach ($your_array as $obj) {
    $uniques[$obj->itemname] = $obj;
}

Open in new window

0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 38859566
Try this if you want to dedupe EXACT duplicates (including duplicate ID and name):

$deduped_array  = array();
foreach($original_array as $object)
{
  $key = sha1(serialize($object));
  $deduped_array[$key] = $object;
}

Open in new window

0
 

Author Comment

by:codeoxygen
ID: 38859699
thank you experts,

myself got it using below one,

this will also re-arrange the array using index

$tmp = array();
foreach($array as $k => $v)
    $tmp[$k] = $v->id;
$tmp = array_unique($tmp);
foreach($array as $k => $v)
{
    if (!array_key_exists($k, $tmp))
        unset($array[$k]);
}
$i=0;
if (count($array) > 0) {
                  foreach ($array as $row1)
                  {
                  $temp[$i]=$row1;
                  $i++;
                  }
}
0
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 2000 total points
ID: 38859733
Okay, I guess that works based off of ID, but it's terribly inefficient. You're looping through your array three different times, applying the array_unique, and also individually removing each element from the original. That's like every time you wash your hands, you go to the sink, examine the first finger on your hand, turn on the water, wash it, turn off the water, examine the second finger, etc...

Just use my code and change the $key to the ID. Then if you want to get the original numeric keys back, just use array_values afterwards:

$deduped_array  = array();
foreach($array as $object)
{
  $deduped_array[$object->id] = $object;
}
$deduped_array = array_values($deduped_array); // Or just ksort($deduped_array); 

Open in new window


Array keys cannot be duplicated, so by simply using the ID as the key in $deduped_array, you will automatically de-dupe as you loop through the array. Anytime it finds a duplicate, it will simply replace the previous one.

Then, array_values() will grab all of the objects and forget about the keys. Personally, I would actually retain the object IDs as the keys (very handy) and just sort those:

ksort($deduped_array);

Open in new window

0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 38859797
did u try my (2) suggestions?
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question