473,398 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Printing a multidimensional Array(Matrix)

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
2 11639
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Ole | last post by:
Goodday everybody, i want to create an array that represents data, that has to be transferred to a pdf doc ( iTextSharp ). The problem is, that i seem too loose my faith. Namely, ( i have...
10
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
8
by: Skay | last post by:
Someone challenged me to create a dynamic multidimensional array using auto_ptr http://www.gotw.ca/gotw/042.htm Provides a good way to create a single dimensional array using an adapter. ...
2
by: xhunga | last post by:
I have try a new version of my work. I have put the sizes of the matrix into the matrix. A = number of rows A = number of columns The first element of the matrix is A instead of A. You...
2
by: Martin Pöpping | last post by:
Hello, I´m new in programming with C# and having some troubles with understanding the definitions of multi-dimensional arrays. Is there any difference between defining an Array with or ? ...
2
by: nleahcim | last post by:
Hi - I am working on writing a number of matrix manipulation functions. The most basic one was a printing algorithm - and it shows the problem I'm having. I'm passing it a pointer a mutidimensional...
2
by: nitinm | last post by:
hi I want to make a program whose requirement are as following: 1) it has to create an NxN matrix after reading input (i.e. N) from a file in the main() itself. 2) it has to send the array as...
0
by: Szabolcs Borsanyi | last post by:
Dear All, there have been several threads on multidimensional arrays and pointers to arrays, there is still something I could not fully understand. (My point here I have raised already, but...
0
by: techuser201 | last post by:
Have developed an application software using VC#.net in vs2008. Have used the inbuilt rdlc reports for creating and managing all reporting info. The problem is that we are having a client base...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.