简体   繁体   English

HTML表中的多维数组

[英]Multidimensional array in html table

I have an array called groupeditems that looks like that: 我有一个名为groupeditems的数组,如下所示:

array(2) {
  ["foo1”]=>
  array(4) {
    [0]=>
    array(20) {
      [0]=>
      string(3) "658"
      ["id"]=>
      string(3) "658"
      [1]=>
      string(12) "Flying Foxes"
      ["song_name"]=>
      string(12) "Flying Foxes"
      [2]=>
      string(4) "Moby"
      ["song_artist"]=>
      string(4) "Moby"
    }
    [1]=>
    array(20) {
      [0]=>
      string(4) "1232"
      ["id"]=>
      string(4) "1232"
      [1]=>
      string(13) "Memory Gospel"
      ["song_name"]=>
      string(13) "Memory Gospel"
      [2]=>
      string(4) "Moby"
      ["song_artist"]=>
      string(4) "Moby"
          }
  }
  [“foo2”]=>
  array(3) {
    [0]=>
    array(20) {
      [0]=>
      string(4) "2047"
      ["id"]=>
      string(4) "2047"
      [1]=>
      string(9) "Yesterday"
      ["song_name"]=>
      string(9) "Yesterday"
      [2]=>
      string(11) "The Beatles"
      ["song_artist"]=>
      string(11) "The Beatles"
          }
     }
}

And I would like to echo this array in an html table so it looks like that : 而且我想在html表中回显此数组,因此它看起来像这样:

<h2> foo1 <h2>

      flying foxes          moby                 
      memory gospel         moby                     

<h2> foo2 <h2>

      yesterday         the beatles        

I am using the following PHP : 我正在使用以下PHP:

foreach ($groupedItems as $mks){
    echo "<table>"; 
     echo '<h2>';
     echo $mks[0];
     echo '</h2>';
         foreach ($mks as $qid=>$rate){
            echo "<tr><td>".$rate[1]."</td><td>".$rate[2]."</td></tr>";
          }
   echo "</table>";
    }

However, $mks[0] is not being displayed, It is supposed to show "foo1" or "foo2" and it just prints "Array" instead. 但是,不会显示$mks[0] ,应该显示“ foo1”或“ foo2”,而只是打印“ Array”。 I am certainly doing something wrong here but I can't figure out what... 我当然在这里做错了,但我不知道是什么...

Thanks for your help 谢谢你的帮助

Try the following code: 尝试以下代码:

foreach ($groupitems as $key => $foo) {
    echo '<h2>' . $key . '</h2>' . '<br>';
    foreach ($foo as $key => $value) {
        echo $value['id'] . $value['song_name'] . $value['song_artist'];
        echo <br>;
    }
}

I'm not sure though why you have the same values twice (once in an integer index and once in a string index). 我不确定为什么您要两次设置相同的值(一次在整数索引中,一次在字符串索引中)。 Before outputting anything you should choose one of the two and stay consistent with it. 在输出任何东西之前,您应该选择两者之一并保持一致。

Cheers. 干杯。

if you modify your array, remove the redundant parts ie 如果您修改阵列,请删除多余的部分,即

  [0]=>           # this is redundant, remove
  string(3) "658" # this is redundant, remove
  ["id"]=>
  string(3) "658"

so your array would look something like this: 所以你的数组看起来像这样:

    Array
(
    [foo1] => Array
        (
            [id] => 658
            [song_name] => flying Floxes
            [song_artist] => moby
        )

    [foo2] => Array
        (
            [id] => 68
            [song_name] => flying
            [song_artist] => mo
        )

)

then do: 然后做:

foreach ($a as $key=>$mks){
    echo "<table>"; 
     echo '<h2>';
     echo $key;
     echo '</h2>';
         foreach ($mks as $qid=>$rate){
            echo "<tr><td>".$qid."</td><td>".$rate."</td></tr>";
          }
   echo "</table>";
    }

hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM