Solved

Eliminate duplicate rows in stdclass in php

Posted on 2013-02-06
5
469 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:sedgwick
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 500 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:sedgwick
ID: 38859797
did u try my (2) suggestions?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

685 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