Need help with looping

I have been trying to use this code to print 10 columns and 10 rows. I get one or the other, can some one help me figure out how to get both. With the
I get 10 rows, with out it I get 10 columns. Will an IF statement help me? thanks

[php]
$array = array(‘1’ => “O”, ‘2’=>‘O’, ‘3’=> ‘$’, ‘4’=>‘M’, ‘5’ => ‘^’);

function populateRoom ($myArray) {
print "$myArray
";
}

for($row = 1; $row <= 10; $row++) {
for($c = 1; $c <= 10; $c++) {
$randomNumber = rand(1,5);
$myArray = $array[$randomNumber];
print $myArray;
populateRoom ($myArray);

}}
[/php]

Well, you created a one-dimensional array and are trying to print it as such.
That would give you one row. Think of your array as one row in excel.
You have headers across the top and one (1) ROW of data in it.

You can loop thru the array and pull out the row of headers (which are called KEYS) along with the data.
This would print two rows or cols. If you really want a grid of data printed, you should create a larger array.
(A multidimensional array.)

So, now that you are mixed up even more… Let us know what you are attempting to do. Are you planning on just using the two rows or are you wanting a larger display? This would make a difference on the way we should explain how to do this.

In a quick brief example… If you only need the key-value pairs such as ‘1’=>“O”, etc… Then, you can parse thru that with a for clause. Like this:
foreach($array as $key=>$val){
// Process $key (‘1’) and $val (‘O’) for each entry in ‘array’…
}
If you need many rows and cols, you must use a multidimensional array such as $array[][]…

Hope that helps…

Sponsor our Newsletter | Privacy Policy | Terms of Service