gpt4 book ai didi

php - 如何在 stdClass 对象数组 PHP 中使用 array_unique 函数

转载 作者:太空宇宙 更新时间:2023-11-04 13:53:18 25 4
gpt4 key购买 nike

我试图获得一个唯一的数组,但我做不到,因为我正在使用一个 stdClass 对象。我的对象名为 $items,它有重复的值。下面是它的一部分。

我们如何用 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 )

最佳答案

您可以像这样删除重复的对象:

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

array_map是 php 中的一个函数,您可以在其中对数组中的每个值应用一个函数(在本例中为 json_encode)。因此,数组中的每个 stdClass Object 都编码为 JSON 字符串。 array_unique然后过滤数组中的每个值并删除所有重复项。在您的情况下,对象可能不是 100% 重复但包含细微差异,您可能应该使用下一个方法。然后我们应用相同的技巧,但使用 json_decode将 JSON 字符串还原为对象。您也可以使用 serializeunserialize用于将对象从字符串编码和解码为字符串。


如果您要删除具有相同 product_id 的对象,您可以这样做。

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

首先我们创建一个空数组,在本例中为 $_tmp(派生自临时数组,因为之后我们不需要它)。然后我们遍历数组。用array_key_exists我们检查数组 ($_tmp) 中是否设置键 ($value->product_id)。然后我们设置将对象分配给具有 product_id 的键。因此,如果有另一个具有相同 product_id 的对象,则不会将其添加到 $_tmp 数组中。最后一行是使用 array_values 将对象分配给 $array所以 key 被重置。

关于php - 如何在 stdClass 对象数组 PHP 中使用 array_unique 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25087458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com