Nested array: how come this prints 10

$nested_arr = [ 1, 2, [3, 4 , 5, [5, 6], 7], [ [8, [9, 10, 11, [12, 13] ], [14], [15, [16, 17, 18], 19 ] ] ] ];
echo $nested_arr[3][0][1][1];

array quiz is this

I did not understand how come it printed 10?

let’s re-write it

[ 1, 2, [3, 4 , 5, [5, 6], 7], [ [8, [9, 10, 11, [12, 13] ], [14], [15, [16, 17, 18], 19 ] ] ] ]

'0' = 1
'1' = 2
'2' = [3, 4 , 5, [5, 6], 7]
'3' = [ [8, [9, 10, 11, [12, 13] ], [14] ] ]


from
'3' = [ [8, [9, 10, 11, [12, 13] ], [14] ] ]

'0' = [8, [9, 10, 11, [12, 13] ], [14] ]

from
'0' = [8, [9, 10, 11, [12, 13] ], [14] ]

'0' = 8
'1' = [9, 10, 11, [12, 13] ]


from
'1' = [9, 10, 11, [12, 13] ]

'0' = 9
'1' = 10

//our answer is 10!

1 Like

@toastedpitabread needless to say, i made mistake while breaking down. can you please suggest any good idea about how to break-down efficiently?

At first I did it by eye, but that’s prone to error.

To double-check, I just opened a window in a text editor and tried breaking it down step by step like this, I think I did it backwards because it seemed like that’s where it was most tangled up.

Pen and paper is also tried-and-true.

1 Like

Why that echos nothing? @toastedpitabread

$nested_arr[0] refers to 1 in $nested_arr. 1 doesn’t have any other values attached to it so you can’t reference to anything inside of it.

so what exactly we are doing / telling to print with that? @toastedpitabread

there is an array on 3, so it will print ‘Array’
[0] will print 1
both [1] will print 2

That is my understanding.

array[0] says to access item at index 0 of array.

array[0][1] says to access item at index 1 of index 0 of array.

array[0][1][2] says to access item at index 2 of index 1 of index 0 of array.

and so forth.

i was missing this @toastedpitabread