0

I am trying to sort an associative array in ascending in order and then transfer it to a HTML table and I am currently stumped at an error. I looked for guidelines here on SO and followed instructions on some posts:

PHP display associative array in HTML table

But still no luck, here is my attempt:

<?php
         function format($g){

          array_multisort($g, SORT_ASC);
          echo "<table>";
          foreach($g as $key=>$row) {
              echo "<tr>";
              foreach($row as $key2=>$row2){
                  echo "<td>" . $row2 . "</td>";
              }
              echo "</tr>";
          }
          echo "</table>";
         }
         $bib = array("Luke"=>"10",
                      "John"=>"30",
                      "Matt"=>"20",
                      "Mark"=>"40");
         format($bib);
       ?>

My debugger is telling me there is an error at my for each loop but I don't see how it is wrong unless there is some syntax error that I am not seeing? The error is saying Invalid argument supplied for foreach()

4
  • 2
    Your array is not multi dimensional, so there is no need for the second foreach-loop. What output structure are you expecting? (I.e. what columns and rows should it produce?) Commented Aug 8, 2016 at 17:33
  • 2
    You're trying to go too deep. The array only has 1 set of key/value pairs. Your second foreach fails because there's nothing to iterate through. Commented Aug 8, 2016 at 17:34
  • you have to check whether $row is an array Commented Aug 8, 2016 at 17:35
  • Use a recursive function or is_array check Commented Aug 8, 2016 at 17:35

2 Answers 2

1

Because your $bib is only single array but you use two foreach to loop this array

At 2nd loop, your $row variable is a string, you can't use foreach for this type

Can you try that for single array ?

<?php
function format($data) {
    array_multisort($data, SORT_ASC);
    echo "<table>";
    foreach($data as $k => $v) {
        echo "<tr>";
        echo "<td>$k</td>";
        echo "<td>$v</td>";
        echo "</tr>";
    }
    echo "</table>";
}

$bib = array("Luke"=>"10",
             "John"=>"30",
             "Matt"=>"20",
             "Mark"=>"40");
format($bib);

?>

$k is Luke John Matt and Mark, $v is 10 30 20 and 40

You can see the foreach example here: http://php.net/manual/en/control-structures.foreach.php

Hope this helpful ^^

Sign up to request clarification or add additional context in comments.

3 Comments

this works, but you have a syntax error or 2, thanks for your help
You are missing a " in your answer, other than that it's useful because it explains the problem. It should be the accepted answer.
Why use _multisort() on a solitary array???
0

You can try this

<?php

function format($data){
    array_multisort($data, SORT_ASC);
    echo "<table>";
    foreach($data as $key => $row) {
        echo "<tr>";
            echo "<td>" . $key . "</td>";
            echo "<td>" . $row . "</td>";
        echo "</tr>";
    }
    echo "</table>";
}

$bib = array(
    "Luke"=>"10", 
    "John"=>"30",
    "Matt"=>"20",
    "Mark"=>"40"
);

format($bib);

?>

1 Comment

I display one item per loop, One td for key and one td value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.