Array accessing problem

Joined
Sep 12, 2022
Messages
39
Reaction score
0
PHP:
I am trying to access Gold in the array below. My answer is echo $treasure_hunt[3][2][1][0]; yet the correct answer is [3][4][2][0];. What's the detailed explanation of this?
$treasure_hunt = ["garbage", "cat", 99, ["soda can", 8, ":)", "sludge", ["stuff", "lint", ["GOLD!"], "cave", "bat", "scorpion"], "rock"], "glitter", "moonlight", 2.11];
 
Joined
Sep 21, 2022
Messages
122
Reaction score
15
PHP:
I am trying to access Gold in the array below. My answer is echo $treasure_hunt[3][2][1][0]; yet the correct answer is [3][4][2][0];. What's the detailed explanation of this?
$treasure_hunt = ["garbage", "cat", 99, ["soda can", 8, ":)", "sludge", ["stuff", "lint", ["GOLD!"], "cave", "bat", "scorpion"], "rock"], "glitter", "moonlight", 2.11];

Nested arrays are hard to read when everything is on one line.

A recursive subroutine can print all the array indexes, to make things clearer.

Code:
[0]  garbage
[1]  cat
[2]  99
[3][0]  soda can
[3][1]  8
[3][2]  :)
[3][3]  sludge
[3][4][0]  stuff
[3][4][1]  lint
[3][4][2][0]  GOLD!
[3][4][3]  cave
[3][4][4]  bat
[3][4][5]  scorpion
[3][5]  rock
[4]  glitter
[5]  moonlight
[6]  2.11
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top