Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Referencing values of an array inside of an array

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #31845
    Ethan27
    Member

    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

    #56867
    MrSoundless
    Member

    I 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

    #56822
    Ethan27
    Member

    Thanks 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…?

    #56760
    MrSoundless
    Member

    An 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.

    #56766
    Ethan27
    Member

    So 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

    #56384
    richtestani
    Member

    You 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

Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘Back End’ is closed to new topics and replies.