I am trying to learn about multidimensional arrays and have deliberately built the below array called $answerStringArray to practice on:
array(4) {
[0] => array(1) {
[0] => array(3) {
[0] => string(8) "Swimming"
[1] => string(7) "Burgers"
[2] => string(3) "Bob"
}
}[1] => array(1) {
[0] => array(3) {
[0] => string(7) "Running"
[1] => string(5) "Chips"
[2] => string(4) "Paul"
}
}[2] => array(1) {
[0] => array(3) {
[0] => string(7) "Jogging"
[1] => string(5) "Salad"
[2] => string(5) "David"
}
}[3] => array(1) {
[0] => array(3) {
[0] => string(7) "Walking"
[1] => string(5) "Sauce"
[2] => string(5) "Frank"
}
}
}
I am trying to build a loop wherby I can echo the values
Swimming
Running
Jogging
Walking
on the first iteration followed by
Burgers
Chips
Salad
Sauce
etc etc
I appreciate I could just write array[0][0][0]; to print Swimming etc etc, but I want to understand how to loop through to get the desired data.
I have tried using a loop as shown below, but it starts getting out of sync and then I realised that maybe that might be a bad way to be trying to do what I am after:
$arrayLength = count($answerStringArray);
for($x = 0; $x <= $arrayLength; $x++){
for ($y =0; $y<=$x; $y++) {
for ($z =0; $z<=$y; $z++) {
echo $answerStringArray[$x][$y][$z];
}
}
}