By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,889 Members | 1,044 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,889 IT Pros & Developers. It's quick & easy.

Printing a multidimensional Array(Matrix)

P: n/a
Hi, i need some help printing a mutlidimensional array, i can do it
for square Matriz MxM but i need to print an MXN matrix.
here is my code :
function printMatrix($matrix){
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
Oct 13 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
serave wrote:
Hi, i need some help printing a mutlidimensional array, i can do it
for square Matriz MxM but i need to print an MXN matrix.
here is my code :
function printMatrix($matrix){
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.

http://www.php.net/manual/en/control...es.foreach.php

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

Scotty
Oct 13 '08 #2

P: n/a
On Oct 14, 3:38*am, serave <ramirez.sebast...@gmail.comwrote:
Hi, i need some help printing a mutlidimensional array, i can do it
for square Matriz MxM but i need to print an MXN matrix.
here is my code :
function printMatrix($matrix){
* * * * 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.

Oct 14 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.