<?php $fruits = [ ['name_id' => 'apple2', 'color' => 'green', 'taste' => 'sweet', 'quantity' => '2', 'price' => 'priceless'], ['name_id' => 'apple1', 'color' => 'red', 'taste' => 'sweet', 'quantity' => '3', 'price' => 'cheap'], ['name_id' => 'apple3', 'color' => 'red', 'taste' => 'sweet', 'quantity' => '1', 'price' => '2'], ['name_id' => 'apple4', 'color' => 'blue', 'taste' => 'another', 'quantity' => '2', 'price' => '1'], ['name_id' => 'apple5', 'color' => 'red', 'taste' => 'bitter', 'quantity' => '1', 'price' => '100'], ['name_id' => 'apple6', 'color' => 'red', 'taste' => 'indefinable', 'quantity' => '6', 'price' => '100'], ['name_id' => 'apple7', 'color' => 'red', 'taste' => 'indefinable', 'quantity' => '2', 'price' => '10'], ['name_id' => 'apple8', 'color' => 'green', 'taste' => 'sour', 'quantity' => '2', 'price' => 'priceless'], ['name_id' => 'apple9', 'color' => 'green', 'taste' => 'sour', 'quantity' => '1', 'price' => 'cheap'] ]; $param1 = 'color'; $param2 = 'taste'; $result = []; foreach ($fruits as $fruit) { $defaults[$fruit[$param2]] = ''; $result[$fruit[$param1]][$fruit[$param2]] = ($result[$fruit[$param1]][$fruit[$param2]] ?? 0) + $fruit['quantity']; } ksort($defaults); ksort($result); echo "<table border=1 cellspacing=0 cellpadding=4>"; echo '<tr><th></th><th>' . implode('</th><th>', array_keys($defaults)) . "</th></tr>\n"; foreach ($result as $p1 => $row) { $row = array_replace(['label' => $p1] + $defaults, $row); echo '<tr><td>' . implode('</td><td>', $row) . "</td></tr>\n"; } echo "</table>";
You have javascript disabled. You will not be able to edit any code.