Liam Delahunty: Home Tips Web Contact
Recommended laptop
under £500
.

Think I deserve a present? See my Amazon Wish List

PHP multi-dimensional arrays

This gets me everytime I try to use a multi-dimensional array in PHP. First a multi dimensional array is an array that has a value of an element as another array.

This means you have an array with some values, and each element in that array can point to futher arrays with indiviusal values. Okay, an example...

Lets say you play the lottery, and you buy two lines of tickets. In this lottery you pick 6 numners for each turn, the first array will be each lottery attempt, while the second array will be the 6 indiviual numbers. In the following snippit I set up the arrays:

$try[] = array("11", "12", "15", "22", "41", "42");
$try[] = array("6", "7", "16", "17", "22", "23");

$count = count ($try);

for ($i=0; $i<$count; $i++){
	$countmore=count($try[0]);
	for ($j=0; $j < $countmore; $j++){
		// this is the bit I allways get wrong..notice how to display the results in the print statement
		print ("i$i j$j " . $try[$i][$j] . "<br> ");
	}
	print ("<br>");
}

So there you have it...

i0 j0 11
i0 j1 12
i0 j2 15
i0 j3 22
i0 j4 41
i0 j5 42

i1 j0 6
i1 j1 7
i1 j2 16
i1 j3 17
i1 j4 22
i1 j5 23

Share this!