-1

I have PHP like this

$associativeArray = array("item1"=>"dogs", "item2"=>"cats",
                          "item3"=>"rats", "item4"=>"bats");

I want to show this data as a HTML table in this same page.

Rolando Isidoro
  • 4,983
  • 2
  • 31
  • 43
mca-surround
  • 87
  • 1
  • 3
  • 10
  • 2
    Well you might want to include: 1. Your attempt 2. Your current output 3. Your expected output (Please also read this: http://stackoverflow.com/help/mcve and http://stackoverflow.com/help/how-to-ask) – Rizier123 Apr 21 '15 at 14:56
  • N-level array into table has answered here https://stackoverflow.com/questions/4746079/how-to-create-a-html-table-from-a-php-array/47015800#47015800 – Delickate Oct 30 '17 at 12:55
  • N-level array into table has answered here https://stackoverflow.com/questions/4746079/how-to-create-a-html-table-from-a-php-array/47015800#47015800 – Delickate Oct 30 '17 at 13:13
  • This thread has been answered here [Click here](https://stackoverflow.com/questions/4746079/how-to-create-a-html-table-from-a-php-array/47068295#47068295) – Delickate Nov 02 '17 at 05:45

4 Answers4

0

You need to loop through your array:

<table>
 <tbody>
   <?php 
     foreach($associativeArray as $key => $index) {
        ?> 
        <tr>
          <td><?php echo $key; ?></td>
          <td><?php echo $index; ?></td>
        </tr>
        <?php
     }
   ?>
 </tbody>
</table>
Daan
  • 12,099
  • 6
  • 34
  • 51
  • 1
    How can you answer the question if you don't even know the desired output from OP? Maybe he wants it all in one row? – Rizier123 Apr 21 '15 at 14:57
  • 1
    `I'm not entirely sure on how to get this array into a HTML table` This is his array in a HTML table. – Daan Apr 21 '15 at 14:59
  • 2
    @Daan `
    =implode($array);?>
    ` this is too
    – kero Apr 21 '15 at 15:01
  • 1
    @Daan so is `
    `
    – hair raisin Apr 21 '15 at 15:02
  • 2
    @Daan ^^ This is what I mean, you don't know how OP wants it. But I don't want to discuss here so I will leave it. Just one last thing to think about: Do you think when answering such question where no effort is shown that OP will put more, less or equal amount of effort into his next question? (<- Don't have to answer it just read it or think about it) – Rizier123 Apr 21 '15 at 15:07
0

Try this Code:

</thead>
<tbody>
<?php 
    foreach ($associativeArray as $key => $value) 
    {
        echo'<tr>'; 
        echo'<td>'. $key .'</td>';
        echo'<td>'. $value .'</td>';
        echo'<tr>';
    }
?>
</tbody>
VijayS91
  • 1,535
  • 23
  • 38
0

In order to loop an associate array it you need something like:

foreach ($array_expression as $key => $value) {
 echo $key;
 echo $value;
}

You should construct your table, before, during and after the loop, i.e.:

Before:

<table>
<tbody>
<tr>

During:

foreach ($array_expression as $key => $value) {
 echo "<td>$value</td>";
}

After:

</tr>
</tbody>
</table>

Your final code may look like this:

<?php
echo "<table><tbody><tr>";
foreach ($array_expression as $key => $value) {
   echo "<td>$value</td>";
}
echo "</tr></tbody></table>";
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • You know what I [think](http://stackoverflow.com/questions/29775778/array-of-strings-in-a-table-with-keys-and-values#comment47682473_29775861) right now, so I will leave it and just say good morning :). (But didn't you missed a dollar sign in your foreach loop for: `array_expression` -> `$array_expression`?!) – Rizier123 Apr 21 '15 at 15:13
  • @Rizier123 Good morning :). I surely do know what you think! but in this case the OP is "only starting out with php" So, I opened an exception. Your words add echo on me, I getting way better :)) Have a wonderful day. – Pedro Lobito Apr 21 '15 at 15:23
0

It is better to use PHP environment to tabulate your array to avoid confusing:

    <?php
    $associativeArray = array("item1"=>"dogs", "item2"=>"cats", "item3"=>"rats", "item4"=>"bats");

    foreach($associativeArray as $index => $value){
        $rows .= "
            <tr>
                <td>$index</td>
                <td>$value</td>
            </tr>
        ";
    }

    print "
        <table>
            <tr>
                <th>#</th>
                <th>value</th>
            </tr>
            $rows
        </table>
        ";
    ?>
Eng Cy
  • 1,527
  • 11
  • 15
  • Why is it better this way? – kero Apr 21 '15 at 15:18
  • 1
    reduces usage of which is confusing and messy and to avoid "header already send" problem when spaces exist outside the PHP tag if not using properly – Eng Cy Apr 21 '15 at 15:26