1

I have an array that includes some other arrays. All values which are inside that array, should be placed in a HTML table. I get all the values but my HTML table looks horrible!

I have a code that looks like this:

<?php

$data = array(
    'name' => array('Tom', 'Robert', 'Julia'),
    'age' => array(32, 45, 21),
    'city' => array('New York', 'Toronto', 'Los Angeles')
);
?>

<table>
    <tr>
        <td>Name</td>
        <td>Age</td>
        <td>City</td>
    </tr>
    <tr>
        <?php
            foreach($data as $values) {
                foreach($values as $value) {
                    echo '<td>' . $value . '</td>';
                }
            }
        ?>
    </tr>
</table>

The output of this code will be:

<table>
  <tr>
    <td>Name</td>
    <td>Age</td>
    <td>City</td>
  </tr>
  <tr>
    <td>Tom</td>
    <td>Robert</td>
    <td>Julia</td>
    <td>32</td>
    <td>45</td>
    <td>21</td>
    <td>New York</td>
    <td>Toronto</td>
    <td>Los Angeles</td>
  </tr>
</table>

This is exactly the output that I need:

<table>
  <tr>
    <td>Name</td>
    <td>Age</td>
    <td>City</td>
  </tr>
  <tr>
    <td>Tom</td>
    <td>32</td>
    <td>New York</td>
  </tr>
  <tr>
    <td>Robert</td>
    <td>45</td>
    <td>Toronto</td>
  </tr>
  <tr>
    <td>Julia</td>
    <td>21</td>
    <td>Los Angeles</td>
  </tr>
</table>

How can I do this?

1
  • I don't really know PHP but the solution seems as simple as wrapping the inner for loop with more echos. ie echo ... foreach(...) {...} echo... Commented Dec 2, 2017 at 16:13

2 Answers 2

1

Short solution with array_map function:

<?php
$data = array(
    'name' => array('Tom', 'Robert', 'Julia'),
    'age' =>  array(32, 45, 21),
    'city' => array('New York', 'Toronto', 'Los Angeles')
);

$items = array_map(null, $data['name'], $data['age'], $data['city']);
?>
<table border='1'>
    <tr><th>Name</th><th>Age</th><th>City</th></tr>
    <?php
        foreach ($items as $v) {
            echo '<tr><td>' . implode('</td><td>', $v) . '</td></tr>';
        }
    ?>
</table>

The output (push Run code snippet button):

<table border="1">
    <tbody><tr><th>Name</th><th>Age</th><th>City</th></tr>
    <tr><td>Tom</td><td>32</td><td>New York</td></tr><tr><td>Robert</td><td>45</td><td>Toronto</td></tr><tr><td>Julia</td><td>21</td><td>Los Angeles</td></tr></tbody></table>

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! That was very helpful!
0

Consider reformatting of the $data array to keep related values close together. It will make your foreach then easier.

  $data = array(
  array('name' => 'Tom', 'age' => 32, 'city' => 'New York'),
  array('name' => 'Robert', 'age' => 45, 'city' => 'Toronto'),
  //...
  );

This way you get the one table data row within one iteration of foreach.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.