3

Im trying to get a unique array but I can't, because I am working with a stdClass Object. My object is named $items and it has duplicate values. Some part of it is below.

How can we do unique array this with php?

Array ( [0] => stdClass Object ( [product_id] => 93 [model] => VNA452E1 [sku] => [upc] => [ean] => [jan] => [isbn] => [mpn] => [location] => [quantity] => 1 [stock_status_id] => 5 [image] => data/1NI_FD_245430_VNA453E1_4_Particolari_BIG.jpg [manufacturer_id] => 0 [shipping] => 1 [price] => 140.0000 [points] => 0 [tax_class_id] => 0 [date_available] => 2014-07-22 [weight] => 0.00000000 [weight_class_id] => 1 [length] => 0.00000000 [width] => 0.00000000 [height] => 0.00000000 [length_class_id] => 1 [subtract] => 1 [minimum] => 1 [sort_order] => 1 [status] => 1 [date_added] => 2014-07-23 16:14:26 [date_modified] => 2014-07-23 16:54:52 [viewed] => 9 [language_id] => 2 [name] => NIKON COOLPIX S02 [description] => <p><span style="font-size: 12pt;"><b>SENSORE</b>:CMOS, 1/3.1, Megapixel Reali : 14,17 , Megapixel Effettivi : 13,20 ; </span></p> <p><span style="font-size: 12pt;"><b>OBIETTIVO</b>: Zoom Ottico : 3 x, Zoom Digitale : 4 x, Apertura Max alla Focale Min : 3,30 F, Apertura Max alla Focale Max : 5,90 F; </span></p> <p><span style="font-size: 12pt;"><b>DISPLAY</b>:2,63 inches; </span></p> <p><span style="font-size: 12pt;"><b>OTTURATORE</b>:1, 1/2000; </span></p> <p><span style="font-size: 12pt;"><b>MEMORIA</b>:7300 mb; </span></p> <p><span style="font-size: 12pt;"><b>REGISTRAZIONE</b>:Risoluzione Max Immagine Foto (Larghezza) : 4160 , Risoluzione Max Immagine Foto </span></p> <p><span style="font-size: 12pt;">(Altezza) : 3120 , Registrazione HD; </span></p> <p><span style="font-size: 12pt;"><b>CONNESSIONI</b>:Connessione HDMI; </span></p> <p><span style="font-size: 12pt;"><b>ALTRE CARATTERISTICHE</b>:Microfono Incorporato, Adattabilità Treppiedi Standard; </span></p> <p><span style="font-size: 12pt;"><b>PESI E MISURE</b>:Altezza : 5,13 cm, Larghezza : 7,71 cm, Profondità : 1,75 cm, 100 g; </span></p> <p><span style="font-size: 12pt;"><b>GARANZIA</b>: 24 mesi.</span></p> <p><br /><br /></p> <p><br /><br /><br /></p> [meta_description] => [meta_keyword] => NIKON COOLPIX S02 [tag] => [category_name] => Cameras [category_id] => 33 ) 

[1] => stdClass Object ( [product_id] => 93 [model] => VNA452E1 [sku] => [upc] => [ean] => [jan] => [isbn] => [mpn] => [location] => [quantity] => 1 [stock_status_id] => 5 [image] => data/1NI_FD_245430_VNA453E1_4_Particolari_BIG.jpg [manufacturer_id] => 0 [shipping] => 1 [price] => 140.0000 [points] => 0 [tax_class_id] => 0 [date_available] => 2014-07-22 [weight] => 0.00000000 [weight_class_id] => 1 [length] => 0.00000000 [width] => 0.00000000 [height] => 0.00000000 [length_class_id] => 1 [subtract] => 1 [minimum] => 1 [sort_order] => 1 [status] => 1 [date_added] => 2014-07-23 16:14:26 [date_modified] => 2014-07-23 16:54:52 [viewed] => 9 [language_id] => 2 [name] => NIKON COOLPIX S02 [description] => <p><span style="font-size: 12pt;"><b>SENSORE</b>:CMOS, 1/3.1, Megapixel Reali : 14,17 , Megapixel Effettivi : 13,20 ; </span></p> <p><span style="font-size: 12pt;"><b>OBIETTIVO</b>: Zoom Ottico : 3 x, Zoom Digitale : 4 x, Apertura Max alla Focale Min : 3,30 F, Apertura Max alla Focale Max : 5,90 F; </span></p> <p><span style="font-size: 12pt;"><b>DISPLAY</b>:2,63 inches; </span></p> <p><span style="font-size: 12pt;"><b>OTTURATORE</b>:1, 1/2000; </span></p> <p><span style="font-size: 12pt;"><b>MEMORIA</b>:7300 mb; </span></p> <p><span style="font-size: 12pt;"><b>REGISTRAZIONE</b>:Risoluzione Max Immagine Foto (Larghezza) : 4160 , Risoluzione Max Immagine Foto </span></p> <p><span style="font-size: 12pt;">(Altezza) : 3120 , Registrazione HD; </span></p> <p><span style="font-size: 12pt;"><b>CONNESSIONI</b>:Connessione HDMI; </span></p> <p><span style="font-size: 12pt;"><b>ALTRE CARATTERISTICHE</b>:Microfono Incorporato, Adattabilità Treppiedi Standard; </span></p> <p><span style="font-size: 12pt;"><b>PESI E MISURE</b>:Altezza : 5,13 cm, Larghezza : 7,71 cm, Profondità : 1,75 cm, 100 g; </span></p> <p><span style="font-size: 12pt;"><b>GARANZIA</b>: 24 mesi.</span></p> <p><br /><br /></p> <p><br /><br /><br /></p> [meta_description] => [meta_keyword] => NIKON COOLPIX S02 [tag] => [category_name] => Fotocamere [category_id] => 33 ) [2] => stdClass Object ( [product_id] => 92 [model] => 1FA6_002 [sku] => [upc] => [ean] => [jan] => [isbn] => [mpn] => [location] => [quantity] => 1 [stock_status_id] => 5 [image] => data/TOM_RG_216169_1FA4_002_02.jpg [manufacturer_id] => 0 [shipping] => 1 [price] => 259.0000 [points] => 0 [tax_class_id] => 0 [date_available] => 2014-07-22 [weight] => 0.00000000 [weight_class_id] => 1 [length] => 0.00000000 [width] => 0.00000000 [height] => 0.00000000 [length_class_id] => 1 [subtract] => 1 [minimum] => 1 [sort_order] => 1 [status] => 1 [date_added] => 2014-07-23 15:55:05 [date_modified] => 2014-07-23 15:58:36 [viewed] => 7 [language_id] => 2 [name] => TOM TOM GO 600 [description] => <p><span style="font-size: 14pt;"><b>SISTEMA GPS</b>:Auto; </span></p> <p><span style="font-size: 14pt;"><b>CARATTERISTICHE GENERALI</b>:Tom Tom, Italia, Austria - Svizzera - Germania, Francia, </span></p> <p><span style="font-size: 14pt;">Penisola Iberica, Regno Unito, Scandinavia, Paesi Est, Russia, Grecia, Major Roads Europe; </span></p> <p><span style="font-size: 14pt;"><b>SCHERMO</b>: 6 "Retroilluminazione, Visualizzazione 3D, 480 Pixels, Risoluzione Verticale 272, </span></p> <p><span style="font-size: 14pt;">Touchscreen; </span></p> <p><span style="font-size: 14pt;"><b>ESPANSIONE / CONNETTIVITÀ</b>:Canali : 12 , POI; </span></p> <p><span style="font-size: 14pt;"><b>CONNETTIVITÀ</b>:Bluetooth;</span></p> <p><span style="font-size: 14pt;"> <b>BATTERIA</b>:120 min; </span></p> <p><span style="font-size: 14pt;"><b>GARANZIA</b>: 24 mesi.</span></p> <p><br /><br /><br /><br /></p> [meta_description] => [meta_keyword] => TOM TOM GO 600 [tag] => [category_name] => Navigatori Satellitari [category_id] => 53 )

2 Answers 2

13

You can remove duplicate objects like this:

$array = array_map('json_encode', $array);
$array = array_unique($array);
$array = array_map('json_decode', $array);

array_map is a function in php where you can apply a function (in this case json_encode) to each value in the array. So every stdClass Object in your array is encoded in a JSON string. array_unique then filters every value in the array and removes any duplicates. In your case the objects where probably not 100% duplicated but contained minor differences and you probably should use the next method. Then we apply the same trick but with json_decode to revert the JSON string to an object. You can also use serialize and unserialize for encoding and decoding the object from/to a string.


In case you to remove the objects with, for example, the same product_id you can do it this way.

$_tmp = array();
foreach($array as $key => $value) {
    if(!array_key_exists($value->product_id),$_tmp) {
        $_tmp [$value->product_id] = $value;
    }
}
$array = array_values($_tmp);

First we create an empty array, in this case $_tmp (derived from temporary, because we don't need it afterwards). Then we loop thru the array. With array_key_exists we check if a key ($value->product_id) is not set in the array ($_tmp). Then we set assign the object to the key with the product_id. So if there is another object with the same product_id, it won't be added to the $_tmp array. The last line is assigning the object to the $array with array_values so the keys are reset.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for suggestion.I tried the first code , it doesnt work.And I couldnt see that where comes from $result in second code?
Yeah i forgot to save the return value of array_unique. The $result var was a bug cause i changed the name of the var in my head... silly me
I appreciate your effort. But I tried the your last code.And it doesnt works too.Are you missing anything in code ? Can you check it again?
Sorry @Ties. Your second solution works perfectly.Thanks a lot. God Bless You :)
But can you explain your code. I couldnt understand
|
0

This might work - $array = (array) $object;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.