I
I
ivanradigin2016-04-08 17:39:05
PHP
ivanradigin, 2016-04-08 17:39:05

How to output data from a multidimensional array to a table?

Compiler

<?php
$names = array ('Петров' => "Петр", 'Иванов' => "Иван", 'Сергеев' => "Сергей");
$FullArray = array (
             'id' => array(1, 2, 3),
             'name' => array ($names)
             );

echo '<table>';
echo '<tr><th>ID</th><th>Имя</th><th>Фамилия</th></tr>';
foreach($FullArray as $key => $massive){
foreach ($massive as $inner_key => $value) 
  echo '<tr><td>'.$value.'</td><td>'.$value.'</td></tr>';
  echo '</table>';
}
//print_r ($FullArray);      
?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Silm, 2016-04-08
@ivanradigin

First, learn how to create these same arrays. You are full of rubbish.

<?php
$arr = [
    ['firsname' => 'Иван', 'lastname' => 'Иванов'],
    ['firsname' => 'Петр', 'lastname' => 'Петров'],
    ['firsname' => 'Сергей', 'lastname' => 'Сергеев'],
]
?>

<table>
<tr><th>ID</th><th>Имя</th><th>Фамилия</th></tr>
<?php foreach($arr as $key => $person): ?>
  <tr>
  <td><?=$key?></td>
  <td><?=$person['firsname']?></td>
  <td><?=$person['lastname']?></td>
  </tr>
<?php endforeach ?>
</table>

M
Max, 2016-04-08
@AloneCoder

And the structure of your array must be like this?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question