Solved

Eliminate duplicate rows in stdclass in php

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

803 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