Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to access an array within another array (basically, a two dimensional array). However, the usual way of doing this ($myarray[0][0]) doesn't work for me. The reason is because I want to create a recursive function which, in each call, should dive deeper and deeper into a array (like, on first call it should look at $myarray[0], on second call it should look at $myarray[0][0] and so on).

Is there any alternative way of accessing an array within array?

Thanks.

share|improve this question
2  
pass along the sub-array as an argument so it's ALWAYS my_passed_in_array[0] no matter how deep you go. –  Marc B Dec 2 '13 at 18:37
    
Alternative way? I've once made a function which worked like this: a(308) got the $a[3][0][8], and it worked since the indexes werent higher than 9. But basically there's no alternative solution. –  Rápli András Dec 2 '13 at 18:38

1 Answer 1

Traverse the array by passing always a subarray of it to the recursive function.

function f(array &$arr)
{
    // Some business logic ...

    // Let's go into $arr[0]
    if(is_array($arr[0]))
        f($arr[0]);
}

f($myarray);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.