<code>array(4) { [0] => array(7) { ["goodsId"] => string(2) "15" ["goodsNo"] => string(13) "9311770592581" ["goodsName"] => string(37) "DCS超级水润泡沫洗面/洁面乳" ["price"] => string(5) "40.00" ["currency"] => string(2) "15" ["quantity"] => int(1) ["goods_tax_price"] => string(1) "0" } [1] => array(7) { ["goodsId"] => string(2) "16" ["goodsNo"] => string(13) "9327693000805" ["goodsName"] => string(21) "DCS超级营养精华" ["price"] => string(5) "10.00" ["currency"] => string(2) "15" ["quantity"] => string(1) "1" ["goods_tax_price"] => string(1) "0" } [2] => array(7) { ["goodsId"] => string(2) "17" ["goodsNo"] => string(13) "9327693000744" ["goodsName"] => string(18) "DCS超级营养水" ["price"] => string(5) "30.00" ["currency"] => string(2) "15" ["quantity"] => string(1) "1" ["goods_tax_price"] => string(1) "0" } [3] => array(7) { ["goodsId"] => string(2) "18" ["goodsNo"] => string(13) "9311770592505" ["goodsName"] => string(27) "DCS集中美白活力面膜" ["price"] => string(5) "40.00" ["currency"] => string(2) "15" ["quantity"] => string(1) "1" ["goods_tax_price"] => string(1) "0" } [4] => array(7) { ["goodsId"] => string(2) "18" ["goodsNo"] => string(13) "9311770592505" ["goodsName"] => string(27) "DCS集中美白活力面膜" ["price"] => string(5) "40.00" ["currency"] => string(2) "15" ["quantity"] => string(1) "2" ["goods_tax_price"] => string(1) "0" } }</code>
Kombinieren Sie die Produktinformationen mit der wiederholten Waren-ID im Array zu einer, und die Zahl ist die Summe der Menge (die Summe der gleichen Produkte)
Das Ergebnis sollte sein:
<code>array(4) { [0] => array(7) { ["goodsId"] => string(2) "15" ["goodsNo"] => string(13) "9311770592581" ["goodsName"] => string(37) "DCS超级水润泡沫洗面/洁面乳" ["price"] => string(5) "40.00" ["currency"] => string(2) "15" ["quantity"] => int(1) ["goods_tax_price"] => string(1) "0" } [1] => array(7) { ["goodsId"] => string(2) "16" ["goodsNo"] => string(13) "9327693000805" ["goodsName"] => string(21) "DCS超级营养精华" ["price"] => string(5) "10.00" ["currency"] => string(2) "15" ["quantity"] => string(1) "1" ["goods_tax_price"] => string(1) "0" } [2] => array(7) { ["goodsId"] => string(2) "17" ["goodsNo"] => string(13) "9327693000744" ["goodsName"] => string(18) "DCS超级营养水" ["price"] => string(5) "30.00" ["currency"] => string(2) "15" ["quantity"] => string(1) "1" ["goods_tax_price"] => string(1) "0" } [3] => array(7) { ["goodsId"] => string(2) "18" ["goodsNo"] => string(13) "9311770592505" ["goodsName"] => string(27) "DCS集中美白活力面膜" ["price"] => string(5) "40.00" ["currency"] => string(2) "15" ["quantity"] => string(1) "3" ["goods_tax_price"] => string(1) "0" } }</code>
<code>array(4) { [0] => array(7) { ["goodsId"] => string(2) "15" ["goodsNo"] => string(13) "9311770592581" ["goodsName"] => string(37) "DCS超级水润泡沫洗面/洁面乳" ["price"] => string(5) "40.00" ["currency"] => string(2) "15" ["quantity"] => int(1) ["goods_tax_price"] => string(1) "0" } [1] => array(7) { ["goodsId"] => string(2) "16" ["goodsNo"] => string(13) "9327693000805" ["goodsName"] => string(21) "DCS超级营养精华" ["price"] => string(5) "10.00" ["currency"] => string(2) "15" ["quantity"] => string(1) "1" ["goods_tax_price"] => string(1) "0" } [2] => array(7) { ["goodsId"] => string(2) "17" ["goodsNo"] => string(13) "9327693000744" ["goodsName"] => string(18) "DCS超级营养水" ["price"] => string(5) "30.00" ["currency"] => string(2) "15" ["quantity"] => string(1) "1" ["goods_tax_price"] => string(1) "0" } [3] => array(7) { ["goodsId"] => string(2) "18" ["goodsNo"] => string(13) "9311770592505" ["goodsName"] => string(27) "DCS集中美白活力面膜" ["price"] => string(5) "40.00" ["currency"] => string(2) "15" ["quantity"] => string(1) "1" ["goods_tax_price"] => string(1) "0" } [4] => array(7) { ["goodsId"] => string(2) "18" ["goodsNo"] => string(13) "9311770592505" ["goodsName"] => string(27) "DCS集中美白活力面膜" ["price"] => string(5) "40.00" ["currency"] => string(2) "15" ["quantity"] => string(1) "2" ["goods_tax_price"] => string(1) "0" } }</code>
Kombinieren Sie die Produktinformationen mit der wiederholten Waren-ID im Array zu einer, und die Zahl ist die Summe der Menge (die Summe der gleichen Produkte)
Das Ergebnis sollte sein:
<code>array(4) { [0] => array(7) { ["goodsId"] => string(2) "15" ["goodsNo"] => string(13) "9311770592581" ["goodsName"] => string(37) "DCS超级水润泡沫洗面/洁面乳" ["price"] => string(5) "40.00" ["currency"] => string(2) "15" ["quantity"] => int(1) ["goods_tax_price"] => string(1) "0" } [1] => array(7) { ["goodsId"] => string(2) "16" ["goodsNo"] => string(13) "9327693000805" ["goodsName"] => string(21) "DCS超级营养精华" ["price"] => string(5) "10.00" ["currency"] => string(2) "15" ["quantity"] => string(1) "1" ["goods_tax_price"] => string(1) "0" } [2] => array(7) { ["goodsId"] => string(2) "17" ["goodsNo"] => string(13) "9327693000744" ["goodsName"] => string(18) "DCS超级营养水" ["price"] => string(5) "30.00" ["currency"] => string(2) "15" ["quantity"] => string(1) "1" ["goods_tax_price"] => string(1) "0" } [3] => array(7) { ["goodsId"] => string(2) "18" ["goodsNo"] => string(13) "9311770592505" ["goodsName"] => string(27) "DCS集中美白活力面膜" ["price"] => string(5) "40.00" ["currency"] => string(2) "15" ["quantity"] => string(1) "3" ["goods_tax_price"] => string(1) "0" } }</code>
Kommen wir ohne weitere Umschweife direkt zum Code:
<code>$arr = array( '0' => array( 'id' => 1, 'count' =>1, ), '1' => array( 'id' => 2, 'count' =>1, ), '2' => array( 'id' => 4, 'count' =>1, ), '3' => array( 'id' => 2, 'count' =>1, ), ); $new = $news = $newss = array(); foreach ($arr as $key => $value) { if(!in_array($value['id'], $new)) { $new[] = $value['id']; //$new保存不重复的id值 $news[$key] = $value; //$news保存不重复id的数组值 $newss[$value['id']] = $key; //$newss保存不重复的id的键值 }else { $k = $newss[$value['id']]; //取出重复的id保存的第一个键值 $count = (int)$news[$k]['count']; //取出第一个相同id保存的count值 $news[$k]['count'] = $count+1; //赋值到新的数组 } } var_dump($news);</code>
Lassen Sie uns über eine Idee sprechen und ein Wörterbuch verwenden.
Da die Waren-ID eindeutig ist, ist k also die Waren-ID.
Durchlaufen Sie das Array. Wenn das Wörterbuch eine Waren-ID enthält, fügen Sie den entsprechenden Wert hinzu. Wenn nicht, geben Sie den entsprechenden Wert ein Daten im Wörterbuch
Entfernen Sie Duplikate aus zweidimensionalen Arrays und prüfen Sie, ob Sie das Ergebnis wünschen: http://zhidao.baidu.com/link?...
Werden diese Daten aus der Datenbank abgerufen? Wenn ja, können Sie sich auf die SQL-Abfrage verlassen, um Duplikate zu entfernen. Wenn sie später zusammengeführt werden, können Sie die PHP-Version der Iteratorklasse finden Dies ist immer der Fall. Es ist unvermeidlich, das Array zu durchlaufen.
Handelt es sich um ein zweidimensionales Array-Deduplizierungsproblem? Wenn ja, habe ich nach dem vorherigen Code gesucht und einmal eine solche Funktion geschrieben.