简体   繁体   English

HTML表中的Echo多维数组

[英]Echo multidimensional array in a html table

I am having a multidimensional array with same values like this, 我有一个具有相同值的多维数组,像这样,

[documents] => Array
(
    [0] => Array
            (
                [doc_id] => 3
                [doc_type] => driving_licence                   
                [expiry_date] => 2015-11-26
                [added_date] => 2015-11-16
            )

    [1] => Array
            (
                [doc_id] => 3
                [doc_type] => driving_licence
                [expiry_date] => 2015-11-26
                [added_date] => 2015-11-16
            )

)

So now I need to echo this array in a html table with single <tr> . 所以现在我需要用单个<tr>在html表中回显此数组。

This is how I tried it : 这是我尝试的方法:

foreach ($userData as $key => $value) {
    $i=0; 
    foreach ($value['documents'] as $k => $v) {
        $i++; 
        $html  = "<tr>\n"; 
        $html .= " <td class='center'>{$i}</td>";
        $html .= " <td>{$v['doc_type']}</td>";
        $html .= " <td>{$v['expiry_date']}</td>";
        $html .= " <td>{$v['added_date']}</td>";
        $html .= "</tr>\n"; 

        echo $html; 
    }
}

But its repeating table <tr> with same values. 但是其重复表<tr>具有相同的值。

Can anybody tell me how to avoid this? 谁能告诉我如何避免这种情况? Thank you. 谢谢。

As requested , here is an example for you. 根据要求 ,这是您的示例。

$userData = $input = array_map("unserialize", array_unique(array_map("serialize", $userData)));

foreach ($userData as $key => $value) {
    $i=0; 
    foreach ($value['documents'] as $k => $v) {
        $i++; 
        $html  = "<tr>\n"; 
        $html .= " <td class='center'>{$i}</td>";
        $html .= " <td>{$v['doc_type']}</td>";
        $html .= " <td>{$v['expiry_date']}</td>";
        $html .= " <td>{$v['added_date']}</td>";
        $html .= "</tr>\n"; 

        echo $html; 
    }
}

That will give you a table with 1 <tr> row. 这将为您提供带有1个 <tr>行的表。

this is the way i would do what you are trying to do: 这就是我将要做的事情的方式:

 $movies = array 
   (
      array('Office Space' , 'Comedy' , 'Mike Judge' ),
      array('Matrix' , 'Action' , 'Andy / Larry Wachowski' ),
      array('Lost In Translation' , 'Comedy / Drama' , 'Sofia Coppola' ),
      array('A Beautiful Mind' , 'Drama' , 'Ron Howard' ),
      array('Napoleon Dynamite' , 'Comedy' , 'Jared Hess' )
   );

   echo "<table border=\\"1\\">";
   echo "<tr><th>Movies</th><th>Genre</th><th>Director</th></tr>";
   for ( $row = 0; $row < count($movies); $row++ )
   {
      echo "<tr><td>";
      for ( $column = 0; $column < count($movies); $column++ )
      {
         echo $movies[$row][$column] ;
      }
      echo "<td></tr>";
   }

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

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