简体   繁体   中英

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. 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.

When you echo your array, you are outputting the key rather than the actual values. Hence why you're getting '0' and '1's in your first column.

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:

$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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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