Getting values of an array inside an array

If I wanted to only reference the one or more of the values of an array inside another array how would I do this…?

I have got the idea of referencing both, and also the first value of the array inside the array, but the second Im not sure about. Would it just be a question of leaving the first square brackets with speechmarks inside it, no value inside it…

Here is some example code in case my wording is a bit confusing.

$array1 = array("cat","fox", "dog", array("horse",shoes","food"));
         echo $array1[1][2];  

If I wanted to just echo “food”, how would this be done…?

Thanks guys

Because your array uses values an no custom keys you will need to use.

echo $array1[3][2];

[3] references to the 4th value and [2] references to food. Also is shoes supposed to be missing the double quote?

Thanks for your reply

Just noticed the missing double quotes, schoolboy error. Not sure what you mean by custom keys, still a bit of a beginner. I do however understand that the values start at 0.

What I meant was how do I reference a value in the second array without referencing a value in the first array…?

Custom keys represent the integers that are generated by the PHP core itself so for instance

$myArray = array('hi', 'bye', 'cya', 'hello');

The above doesn’t use any custom keys there for the PHP core will generate keys starting with the integer of 0.

$myArray = array('s1' => 'hi', 's2' => 'bye', 's3' => 'cya', 's4' => 'hello');

The above has custom keys (s1, s2, s3 and s4) which reference to the values (hi, bye, cya and hello). Hope that makes more sense now.

To reference to the array inside your array you need to reference the key or use a foreach() loop to find an array the seek out a value you need.

Thanks dude that has really helped.

The custom key layout seems very similar to something Ive just read about called “assosiative array”, are they similar…?

Once again thanks man, you know I thought about just leaving the first brackets in the echo statement as blank therefore only showing the second called value but I guess its not as easy as that lol (:

Sorry yes its an associative array, i have been coding heaps of HTML recently and have forgotten names for simple PHP :blush:

Arrays can be quite difficult at first to work out as they took me about 1 month to fully understand and work with correctly before i could use them in development code without any help.

Glad it helped you out though :stuck_out_tongue: