2

Looping through a multidimensional array requires foreach loop in PHP usually so I was wondering can it be done using the

for(int $i=0;i<3;$i++)

format.

example of multidimensional array:

$array = array (array(3,4,5,7), array('r', 'g', 'q','c'));
0

2 Answers 2

3
$array = [ [3, 4, 5, 7], ['r', 'g', 'q', 'c'] ];

array_walk_recursive(
    $array,
    function(&$value, &$key) {
        echo "$key => $value\n";
    }
);
Sign up to request clarification or add additional context in comments.

Comments

1

Yes, you could use a for or a foreach.

For example:

$array = array (array(3,4,5,7), array('r', 'g', 'q','c'));
for($x=0; $x < 2; $x++) {
    for($i=0;$i<4;$i++) {
        echo $array[$x][$i];
    }
}

Foreach example:

$array = array (array(3,4,5,7), array('r', 'g', 'q','c'));
foreach($array as $parent) {
    foreach($parent as $values) {
        echo $values;
    }
}

Output:

3457rgqc

2 Comments

if you didn't know the length of the arrays, instead of $i<4 what could you use instead. $x<count($array)-1 perhaps?
You could put in count($array[$x]). This is not the best for performance though. Take a look at this thread: stackoverflow.com/questions/3430194/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.