- This topic is empty.
-
AuthorPosts
-
March 1, 2011 at 11:49 pm #31845Ethan27Member
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 I think and also the first one but the second Im not sure about. Would it just be a question of leaving the first square brackets with only speechmarks, 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
March 3, 2011 at 7:00 pm #56867MrSoundlessMemberI don’t get your whole story but to answer your last question: echo $array1[3][2];
If that doesn’t help, try to explain what you are trying to do.
Good luck
March 3, 2011 at 10:52 pm #56822Ethan27MemberThanks for your reply
I want to reference the values of the array inside the array
Therefore in this case I only want to echo “food”, nothing else.
I noticed you put ‘3’ in the first reference. Is this because there is no ‘3’ value and this is a simple workaround…?
March 4, 2011 at 2:10 am #56760MrSoundlessMemberAn array starts counting by 0. So by using 3 in the first reference, you refer to the 4th element in the first array. Now since the 4th element in the array is an array you can add a second reference, since food is the 3th element in that array, you use 2 as the 2nd reference.
March 4, 2011 at 4:40 am #56766Ethan27MemberSo since the third element is an array you write “3” then reference the value right…?
The value in the first set of brackets, to put in a basic way doesnt have anything to do with the values in the first array, excluding the fact you’re referncing the second array…?
Thanks man
March 6, 2011 at 4:45 pm #56384richtestaniMemberYou should loop though them.
foreach($array as $val) {
if(is_array($val) {
print_r($val);
}
}
You could even loop back though it by doing this in a function:
function my_array($array) {
foreach($array as $val) {
if(is_array($val) {
my_array($array);
} else {
echo '- '.$val.'';
}
}
}my_array($array);sorry for formatting, not sure how to get it pretty
- '.$val.'';
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.