简体   繁体   English

PHP多维数组到html表

[英]PHP multidimensional array to html table

I have an multidimensional array which I'm trying to output as a table, here is my array; 我有一个多维数组,试图将它输出为表格,这是我的数组;

$marksarray=     
array(3) {
      [0]=>
      array(2) {
        [0]=>
        string(1) "8"
        [1]=>
        string(1) "0"
      }
      [1]=>
      array(2) {
        [0]=>
        string(1) "9"
        [1]=>
        string(1) "1"
      }
      [2]=>
      array(2) {
        [0]=>
        string(2) "13"
        [1]=>
        string(1) "2"
      }
    }

So far I have my code like this; 到目前为止,我的代码是这样的;

echo "<table><tr><td>Question</td><td>Rating</td></tr>";
     foreach ($marksarray as $mks){
         foreach ($mks as $qid=>$rate){
            echo "<tr><td>".$qid."</td><td>".$rate."</td></tr>";
          }
    }
echo "</table></div>";

But my output is; 但是我的输出是 在此处输入图片说明

What is that i'm doing wrong? 我做错了什么?

You've got one too many foreach 's going on there. 你有太多foreach正在进行。 Try this instead: 尝试以下方法:

echo "<table><tr><td>Question</td><td>Rating</td></tr>";
     foreach ($marksarray as $mks){
        echo "<tr><td>".$mks[0]."</td><td>".$mks[1]."</td></tr>";
    }
echo "</table></div>";

Edit 编辑

For future reference, it makes your code far easier to understand if you use an array of associative arrays with meaningful keys. 供以后参考,如果您使用带有有意义键的关联数组的数组,它将使您的代码更容易理解。 eg 例如

$marksarray = array(
    array('qid' => 8, 'rating' => 0), 
    array('qid' => 9, 'rating' => 1), 
    array('qid' => 13, 'rating' => 2)
);

Then your loop would look like this: 然后,您的循环将如下所示:

foreach ($marksarray as $mark){
    echo "<tr><td>".$mark['qid']."</td><td>".$mark['rating']."</td></tr>";
}

Better still, you should use MVC (Model, View, Controller) and pass this data into a view...but that's another subject entirely. 更好的是,您应该使用MVC(模型,视图,控制器)并将此数据传递到视图中……但这完全是另一个主题。

When you echo your array, you are outputting the key rather than the actual values. 当您echo数组时,您将输出key而不是实际值。 Hence why you're getting '0' and '1's in your first column. 因此,为什么在第一栏中输入“ 0”和“ 1”。

If you are stuck with the array layout that you currently have, you want the following code: 如果您卡在当前的数组布局中,则需要以下代码:

echo "<table><tr><td>Question</td><td>Rating</td></tr>";
     foreach ($marksarray as $mks){
        echo "<tr><td>".$mks[0]."</td><td>".$mks[1]."</td></tr>";
     }
echo "</table></div>";

... so that you are making use of the key values to pull out the matching values that you want to show. ...,以便您利用键值提取要显示的匹配值。

If you're not stuck with the array structure that you have now, you'd have to structure your array like so, to make use of the key and pair values: 如果您不拘泥于现在的数组结构,则必须像这样构造数组,以利用keypair值:

$marksarray = array(
    "8" => 0,
    "9" => 1,
    "13" => 2,
);

and use the code: 并使用代码:

echo "<table><tr><td>Question</td><td>Rating</td></tr>";
     foreach ($marksarray as $qid => $rate){

        echo "<tr><td>".$qid."</td><td>".$rate."</td></tr>";

     }
echo "</table></div>";

... this way you are making clear reference to your key and pair values within your code. ...这样一来,您可以清楚地引用代码中的keypair值。

change the echo line to 将回声线更改为

   echo "<tr><td>".$rate[0]."</td><td>".$rate[1]."</td></tr>";

Is it working? 工作正常吗?

This is outputting correctly, and your loops look fine. 这是正确输出,并且您的循环看起来很好。 What I think you mean to do in your array is something like: 我认为您打算在阵列中执行的操作类似于:

$marksarray = array(
    "8" => "0",
    "9" => "1",
    "13" => "2"
);

Then change your loop to: 然后将循环更改为:

echo "<table><tr><td>Question</td><td>Rating</td></tr>";
         foreach ($marksarray as $qid=>$rate){
            echo "<tr><td>".$qid."</td><td>".$rate."</td></tr>";
          }
echo "</table></div>";

I know this is a little old, but I believe this code will do exactly what you want with the existing array. 我知道这有点旧,但是我相信这段代码将完全按照您想要的现有数组进行操作。

echo "<table><tr><td>Question</td><td>Rating</td></tr>";
 foreach ($marksarray as $mks){
     echo "<tr>";
     foreach ($mks as $qid=>$rate){
        echo "<td>".$rate."</td>";
      }
      echo "</tr>";
}

echo "</table></div>";

Is there any reason not to do it this way? 有什么理由不这样做吗?

try using this code 尝试使用此代码

function printmarraytable($data){
                            echo "<table>";
                            foreach($data as $key=>$value){
                                echo "<tr><td>".$key."</td>";
                                if(is_array($value) || is_object($value)){
                                    echo "<td>".printmarraytable($value)."     </td>";
                                }else{
                                    echo "<td>".$value."</td></tr>";
                                }
                            }
                            echo "</table>";
                          }
                          printmarraytable($req);

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

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