There is a multidimensional array for example:

<?php $prod = array( array( "Название" => "Первый продукт", "Производитель" => "Япония", "Год выпуска" => 2016 ), array( "Название" => "Второй продукт", "Производитель" => "Корея", "Год выпуска" => 2016 ), array( "Название" => "Третий продукт", "Производитель" => "Россия", "Год выпуска" => 2014 ) ); echo '<table><tbody>'; foreach($prod as $value){ echo '<tr><td>'; foreach($value as $key => $val){ echo '<td>'.$key .'=>'. $val.'</td>'; } echo '</td></tr>'; } echo '</tbody></table>'; ?> 

I want to make a table of it, in which each row will be a separate characteristic, and each cell in this row should be one of the variants of this characteristic.

That is, in the first line I had 3 cells with names, in the second - with manufacturers, and in the third year, and so on. the number of characteristics I have is constantly changing, the brain broke, it turned out different, and only today I realized that the characteristics should be recorded line by line, and the maximum that happened was to write a separate characteristic into a separate cell, which is absolutely not good.

It should turn out like this: enter image description here

  • That is, if you have 1000 manufacturers in the array, then in the first row of the table there should be 1000 cells with their names? - Alexey Shimansky
  • A clear explanation, an attempt to solve, an example of the correct conclusion - am I in paradise of questions? :) Even a plus for joy. - user207618
  • Yes, that's right, this is a feature comparison table. People are used to comparing the characteristics across, but I don’t think how horizontally to get rid of - Yevgeny Shevtsov
  • @Other I would like to go to the paradise of answers)) - Evgeny Shevtsov
  • I'll sketch it out now. - user207618

1 answer 1

 $prod = [ [ "Название" => "Первый продукт", "Производитель" => "Япония", "Год выпуска" => 2016 ], [ "Название" => "Второй продукт", "Производитель" => "Корея", "Год выпуска" => 2016 ], [ "Название" => "Третий продукт", "Производитель" => "Россия", "Год выпуска" => 2014 ], [ "Название" => "X продукт", "Год выпуска" => 2042 ] ]; $data = []; foreach($prod as $item){ foreach(array_keys($item) as $key){ // Если ключа нет, создаём его if(!array_key_exists($key, $data)) $data[$key] = []; // Добавляем к ключу ещё один элемент $data[$key][] = $item[$key]; } } $table = '<table>'; foreach($data as $th => $td){ // Открываем новую строку с названием $row = '<tr><th>' . $th . '</th>'; // Если у текущего набора нет каких-то элементов (просто не передали), дополняем пустой строкой (можно написать ) // Можно оставить пустую строку, но для создания валидной таблицы заполнить надо (td создаются) if(count($td) - 1 < count($prod)) $td = array_merge($td, array_fill(0, count($prod) - count($td), '<em>No data</em>')); // Далее пояснять, надеюсь, не нужно foreach($td as $cell){ $row .= '<td>' . $cell . '</td>'; } $table .= $row . '</tr>'; } $table .= '</table>'; var_dump($table); 

https://repl.it/GHh9/0