user3004208
user3004208

Reputation: 559

Multi diemensional array foreach key item php

Array

    [1] => Array
        (
            [0] => Array
                (
                    [id] => 17
                    [model] => SB125T-23B
                    [file] => SB125T-23B_Blue
                    [color] => Blue
                    [hex] => 0033ff-3c3c3c
                    [active] => 1
                )

            [1] => Array
                (
                    [id] => 18
                    [model] => SB125T-23B
                    [file] => SB125T-23B_Red
                    [color] => Red
                    [hex] => CC0000-3c3c3c
                    [active] => 1
                )

        )

PHP Code

foreach ($threeSixty[0] as $key => $value) { 
    foreach ($value as $k => $v) { 
        echo $threeSixty[$key][$v];
    }
}

I have this array and PHP code. i am trying to loop through the outer array to get to the inner arrays. Then looping through those i am trying to get to the data under file. Doing it how i have it at the moment just returns the value for each data item. so id then model and so forth.

how can i do it so i can access the file so echo $threeSixty[0]['file']; for example.

Upvotes: 0

Views: 78

Answers (1)

madebydavid
madebydavid

Reputation: 6517

You're using your value $v as a key - use $k instead. Also, unless you have an array in an array in an array - don't specify the index in the first loop. (I think you have an array in an array :-)

foreach ($threeSixty as $key => $value) { 
    foreach ($value as $k => $v) { 
        echo $threeSixty[$key][$k];
    }
}

To get just file:

foreach ($threeSixty as $key => $value) { 
    echo $threeSixty[$key]['file'];
}

To get file for only one:

echo $threeSixty[0]['file'];

Edit: I am assuming that $threeSixty looks something like this:

$threeSixty = array(
    array(
        'id' => '17',
        'model' => 'SB125T-23B',
        'file' => 'SB125T-23B_Blue',
        'color' => 'Blue',
        'hex' => '0033ff-3c3c3c',
        'active' => '1'
    )
);

Upvotes: 1

Related Questions