Multi Array in table html

Hello there,

I have something is really get me a big headache here’s my issue

i have an multi array:-

array (size=3)
  0 => 
    array (size=5)
      0 => string '1' (length=1)
      1 => string '2' (length=1)
      2 => string '3' (length=1)
      3 => string '4' (length=1)
      4 => string '5' (length=1)
  1 => 
    array (size=5)
      0 => string '1' (length=1)
      1 => string '2' (length=1)
      2 => string '3' (length=1)
      3 => string '4' (length=1)
      4 => string '5' (length=1)
  2 => 
    array (size=5)
      0 => string '1' (length=1)
      1 => string '2' (length=1)
      2 => string '3' (length=1)
      3 => string '4' (length=1)
      4 => string '5' (length=1)

I want to echo them as follow in a table html

Table
name | price | quant
1 |1 |1
2 |2 |2
3 |3 |3
4 |4 |4
5 |5 |5

I can’t get it work with foreach cuz i getting all value in the same tr for every array

Please Help !

Can you show us the code you have now?

Scott

$cols = count($array);
$rows = count($array[0]);

echo '<table>';

for ($r=0; $r < $rows; $r++){

    echo '<tr>';

    for ($c=0; $c < $cols; $c++){
        echo '<td>' . $array[$c][$r] . '</td>';
    }

    echo '</tr>';

}

echo '</table>';
1 Like

This works for me:

foreach($array as $element) {
  var_dump($element);
}

But you have to bear in mind that foreach() interates through each array within the main array, so it’s a little more complex to arrange it in the html output you want - getting the table “sideways” is really easy, getting it the way you want it will require building up each row then merging them at the end. So the code @megazoid showed is probably much easier unless you have a specific reason for using foreach().

Can’t find a word to thanks u with @megazoid , BIG THNAKS :joy:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.