How to use array_unique function in stdClass Object Array PHP

I am trying to get a unique array, but I cannot, because I am working with a stdClass object. My object is named $items and has duplicate values. Part of it is below.

How can we make a unique array using 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 ) 
0
source share
2 answers

You can remove duplicate objects as follows:

 $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 ) for each value in the array. Therefore, every stdClass Object in your array is encoded in a JSON string. array_unique then filters each value in the array and removes any duplicates. In your case, objects where they are probably not 100% duplicated but contain slight differences, and you should probably use the following method. Then we apply the same trick, but with json_decode , to return the JSON string to the object. You can also use serialize and unserialize to encode and decode an object from / to a string.


If you delete objects, for example, with 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 (obtained from a temporary one, because we do not need it). Then we iterate over the array. With array_key_exists we check if the key ( $value->product_id ) is set not in the array ( $_tmp ). Then we will assign the object to the key using product_id. Therefore, if there is another object with the same product_id, it will not be added to the $_tmp . The last line assigns a $array object with array_values , so the reset keys.

+6
source

It may work - $ array = (array) $ object;

0
source

Source: https://habr.com/ru/post/1262335/


All Articles