Solved

Eliminate duplicate rows in stdclass in php

Posted on 2013-02-06
5
457 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 34

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 34

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
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…
The viewer will learn how to dynamically set the form action using jQuery.
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now