How is the array indexed?

Im so lost here someone help.

$treasure_hunt = [“garbage”, “cat”, 99, [“soda can”, 8, “:)”, “sludge”, [“stuff”, “lint”, [“GOLD!”], “cave”, “bat”, “scorpion”], “rock”], “glitter”, “moonlight”, 2.11];

how is the arrays indexed?

Hi @raingamluikham,

The indices on an array only go one level deep, so the deeper nested the item you want to select, the more sets of square brackets you have to use to select it.

In this example, $arr has only four direct children in the array (that means that $arr[3] is the last item). To select something from the third item ($arr[2]), which is also an array, you’d have to use a second set of square brackets after the first set, like this: $arr[2][0].

$arr = ["apples", "plums", ["banannas", [50, 60, 70], "oranges"], "pears"]

count($arr) # 4
$arr[0] # "apples"
$arr[2] # ["banannas", [50, 60, 70], "oranges"]
$arr[2][1] # [50, 60, 70]
$arr[2][1][2] # 70
$arr[3] # "pears"
4 Likes