Processing of php array duplicate data

Source: Internet
Author: User
{Code...} combines the repeated goodsId product information in the array. The result is: {code ...}
Array (4) {[0] => array (7) {["goodsId"] => string (2) "15" ["goodsNo"] => string (13) "9311770592581" ["goodsName"] => string (37) "DCS super moisturizing foam cleanser/cleanser" ["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 Super nutrition essence" ["price"] => string (5) & quot; 10.00 & quot; [& quot; currency & quot;] => string (2) & quot; 15 & quot; [& quot; quantity & quot;] = & quot; string (1) "1" ["goods_tax_price"] => string (1) "0"} [2] => array (7) {["goodsId"] => string (2) "17" ["goodsNo"] => string (13) "9327693000744" ["goodsName"] => string (18) "DCS Super nutrition" ["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 concentrated whitening dynamic mask" ["price"] => string (5) & quot; 40.00 & quot; [& quot; currency & quot;] => string (2) & quot; 15 & quot; [& quot; quantity & quot;] = & quot; string (1) "1" ["goods_tax_price"] => string (1) "0"} [4] => array (7) {["goodsId"] => string (2) "18" ["goodsNo"] => string (13) "9311770592505" ["goodsName"] => string (27) "DCS concentrated whitening dynamic mask" ["price"] => string (5) "40.00" ["currency"] => string (2) "15" ["quantity"] => string (1) "2" ["goods_tax_price"] => string (1) "0 "}}

Combine the repeated goodsId product information in the array, and the number is the sum of the quantity (same product sum)

The result should be:

Array (4) {[0] => array (7) {["goodsId"] => string (2) "15" ["goodsNo"] => string (13) "9311770592581" ["goodsName"] => string (37) "DCS super moisturizing foam cleanser/cleanser" ["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 Super nutrition essence" ["price"] => string (5) & quot; 10.00 & quot; [& quot; currency & quot;] => string (2) & quot; 15 & quot; [& quot; quantity & quot;] = & quot; string (1) "1" ["goods_tax_price"] => string (1) "0"} [2] => array (7) {["goodsId"] => string (2) "17" ["goodsNo"] => string (13) "9327693000744" ["goodsName"] => string (18) "DCS Super nutrition" ["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 concentrated whitening dynamic mask" ["price"] => string (5) & quot; 40.00 & quot; [& quot; currency & quot;] => string (2) & quot; 15 & quot; [& quot; quantity & quot;] = & quot; string (1) "3" ["goods_tax_price"] => string (1) "0 "}}

Reply content:
Array (4) {[0] => array (7) {["goodsId"] => string (2) "15" ["goodsNo"] => string (13) "9311770592581" ["goodsName"] => string (37) "DCS super moisturizing foam cleanser/cleanser" ["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 Super nutrition essence" ["price"] => string (5) & quot; 10.00 & quot; [& quot; currency & quot;] => string (2) & quot; 15 & quot; [& quot; quantity & quot;] = & quot; string (1) "1" ["goods_tax_price"] => string (1) "0"} [2] => array (7) {["goodsId"] => string (2) "17" ["goodsNo"] => string (13) "9327693000744" ["goodsName"] => string (18) "DCS Super nutrition" ["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 concentrated whitening dynamic mask" ["price"] => string (5) & quot; 40.00 & quot; [& quot; currency & quot;] => string (2) & quot; 15 & quot; [& quot; quantity & quot;] = & quot; string (1) "1" ["goods_tax_price"] => string (1) "0"} [4] => array (7) {["goodsId"] => string (2) "18" ["goodsNo"] => string (13) "9311770592505" ["goodsName"] => string (27) "DCS concentrated whitening dynamic mask" ["price"] => string (5) "40.00" ["currency"] => string (2) "15" ["quantity"] => string (1) "2" ["goods_tax_price"] => string (1) "0 "}}

Combine the repeated goodsId product information in the array, and the number is the sum of the quantity (same product sum)

The result should be:

Array (4) {[0] => array (7) {["goodsId"] => string (2) "15" ["goodsNo"] => string (13) "9311770592581" ["goodsName"] => string (37) "DCS super moisturizing foam cleanser/cleanser" ["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 Super nutrition essence" ["price"] => string (5) & quot; 10.00 & quot; [& quot; currency & quot;] => string (2) & quot; 15 & quot; [& quot; quantity & quot;] = & quot; string (1) "1" ["goods_tax_price"] => string (1) "0"} [2] => array (7) {["goodsId"] => string (2) "17" ["goodsNo"] => string (13) "9327693000744" ["goodsName"] => string (18) "DCS Super nutrition" ["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 concentrated whitening dynamic mask" ["price"] => string (5) & quot; 40.00 & quot; [& quot; currency & quot;] => string (2) & quot; 15 & quot; [& quot; quantity & quot;] = & quot; string (1) "3" ["goods_tax_price"] => string (1) "0 "}}

Let's talk about the code directly:

$ 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 save the unique id value $ news [$ key] = $ value; // $ news: save the array value of unique IDs $ newss [$ value ['id'] = $ key; // $ key value for saving unique IDs in newss} else {$ k = $ newss [$ value ['id']; // retrieve the first key value saved by the duplicate id $ count = (int) $ news [$ k] ['count']; // retrieve the first count value saved with the same id $ news [$ k] ['count'] = $ count + 1; // assign a value to a new array} var_dump ($ news );

Let's talk about one idea. use a dictionary.
Because goodsId is unique, k is goodsId
Traverse the array. if goodsId is in the dictionary, add the corresponding value. If no value exists, put the corresponding data in the dictionary.

Two-dimensional array deduplication, see if you want a result: http://zhidao.baidu.com/link ?...

Are these data retrieved from the database? if so, re-reading can depend on SQL query de-duplicating. if merge is later, you can find PHP version iterator classes, the operation can be simpler, but it will never be able to traverse the array.

Is this a two-dimensional array de-duplication problem? If so, I looked for the previous code and wrote such a function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.