Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Eliminate duplicate rows in stdclass in php

Posted on 2013-02-06
5
Medium Priority
?
482 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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:sedgwick
ID: 38859797
did u try my (2) suggestions?
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

722 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