Printing a multidimensional Array(Matrix)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • serave

    Printing a multidimensional Array(Matrix)

    Hi, i need some help printing a mutlidimensiona l array, i can do it
    for square Matriz MxM but i need to print an MXN matrix.
    here is my code :
    function printMatrix($ma trix){
    echo "<table width=\"200\" border=\"1\">";
    foreach($matrix as $row =$rValue){
    echo "<tr>";
    foreach($rValue as $col =$cValue){
    echo "<td>".$cValue. "</td>";
    }
    echo "</tr>";
    }
    echo "</table>";
    }

    and this is the message i get:
    Warning: Invalid argument supplied for foreach()

    please help me
  • FutureShock

    #2
    Re: Printing a multidimensiona l Array(Matrix)

    serave wrote:
    Hi, i need some help printing a mutlidimensiona l array, i can do it
    for square Matriz MxM but i need to print an MXN matrix.
    here is my code :
    function printMatrix($ma trix){
    echo "<table width=\"200\" border=\"1\">";
    foreach($matrix as $row =$rValue){
    echo "<tr>";
    foreach($rValue as $col =$cValue){
    echo "<td>".$cValue. "</td>";
    }
    echo "</tr>";
    }
    echo "</table>";
    }
    >
    and this is the message i get:
    Warning: Invalid argument supplied for foreach()
    >
    please help me
    Means you are not giving foreach an array argument.



    Show the code that creates the $matrix array and the printMatrix() call
    and we can go further into your problem.

    Scotty

    Comment

    • CyberSprinters

      #3
      Re: Printing a multidimensiona l Array(Matrix)

      On Oct 14, 3:38 am, serave <ramirez.sebast ...@gmail.comwr ote:
      Hi, i need some help printing a mutlidimensiona l array, i can do it
      for square Matriz MxM but i need to print an MXN matrix.
      here is my code :
      function printMatrix($ma trix){
              echo "<table width=\"200\" border=\"1\">";
              foreach($matrix as $row =$rValue){
                      echo "<tr>";
                      foreach($rValue as $col =$cValue){
                              echo "<td>".$cValue. "</td>";
                      }
                      echo "</tr>";
              }
              echo "</table>";
      >
      }
      >
      and this is the message i get:
      Warning: Invalid argument supplied for foreach()
      >
      please help me


      I used follwing simple code to test the function:

      $a=array();
      for($i=1;$i<=5; $i++)
      {
      for($j=1;$j<=5; $j++)
      {
      $a[$i][$j]=$i+$j;
      }
      }
      printMatrix($a) ;

      and it is working fine for me.

      So it might be a chance that the array is not 2 dimensional in your
      case.

      Comment

      Working...