Good afternoon. I fight not the first day of the task ...

Displays the goods in the form of a table:

Name Code Quantity

Through the foreach loop, the repeating item is displayed in the table, but it is necessary that the entire repeating item (by name and code) is no longer output, and the number is added to what is already there.

<?php foreach ($product_data as $product) { ?> <tr> <td><?php echo $product['name']; ?></td> <td><?php echo $product['sku']; ?></td> <td class="text-right"><?php echo $product['quantity']; ?></td> </tr> <?php } ?> 

Here is my code. Tell me, please, how to be?

Update

My attempts:

 if(!in_array($product['name'], $sum)) { $sum['name'][] = $product['name']; } if(!in_array($product['sku'], $sum)) { $sum['sku'][] = $product['sku']; } $sum['name'] = array_map("unserialize", array_unique( array_map("serialize", $sum['name']))); $sum['sku'] = array_map("unserialize", array_unique( array_map("serialize", $sum['sku']))); 

Accordingly, I declared $sum above as an array. Collects everything as it should. Only 1 name remains, but how does the quantity combine the same?

  • I do not see a single condition where you are assembling a new assembled array. Tried to do something yourself? - Vasily Barbashev
  • Add this to your question, through the "Edit", it is not clear what you wrote here - Vasily Barbashev

2 answers 2

Alexander, of course, you can’t tell a lot about your code ... It’s not clear where $ product_data array comes from ... so it's hard to advise.

1) If you take this $ product_data from the database, you can safely correct the request and get all the values ​​that are needed ... for example, if we have fields:

"id | name | quantity",

then we can pick them up like this:

"SELECT id, name, SUM (quantity) as quantity_total FROM mytable GROUP BY id"

2) If you are dealing with only an array, then it must first be prepared before output.

 $product_data_new = array(); foreach ($product_data as $product) { if (!isset($product_data_new[$product['name']])) { $product_data_new[$product['name']] = $product; } else { $product_data_new[$product['name']]['quantity'] += $product['quantity']; } } 

and then output your resulting $ product_data_new :

 <?php foreach ($product_data_new as $product) { ?> <tr> <td><?php echo $product['name']; ?></td> <td><?php echo $product['sku']; ?></td> <td class="text-right"><?php echo $product['quantity']; ?></td> </tr> <?php } ?> 

3) In general, for good, it is better to immediately fix the presence of such a product in the already added ones and increase the quantity, rather than adding another element of the array.

I do not pretend to the amazing beauty and elegance of the proposed solutions. As the song says: "Think for yourself, decide for yourself ..." =)))

  • Thank you very much for solving the issue. - Alexander

Make in the query group by (name and code) and quantity, choose so: summ (quantity) as quantity. But it seems to me that this is somehow wrong in terms of the architecture of the database.

  • For some reason, I did not see the varinat Stanislav. He suggests the same thing, but groups by id, not by name. - ilyaplot
  • one
    mc wrote that you can group by name or by code =) in any case, I signed that my answer is an example, introducing which I still need to think about, and, most likely, correct, because Initially there is little input in the question - Stanislav
  • Yes, it does not matter, ts chose array_unique + array_map inside array_map. Sometimes the audience of this resource is amazing. - ilyaplot
  • ilyaplot, thank you for responding. Regarding the fact that I went through array_unique + array_map, that is not a deliberate action and I do not pretend to something. It was just one of the options that I found on the Runet’s space ... Forgive me if this somehow offends you as a programmer, since I am an amateur and my experience in PHP is very small. I encountered this type of task for the first time and tried to build a logical chain myself, but I didn’t know from a technical point of view how to do it in the final version. - Alexander
  • Your decisions will not hurt me personally. Just think that getting data in large quantities than is required, and then still multiple cyclic processing of this data is the most resource-intensive option. A good programmer is a lazy programmer. Learn to write as little as possible the best code possible. - ilyaplot