johnsmith
johnsmith

Reputation: 15

php - how to get this value from multidemnsional array

$resolutions = array(
    '4:3' => array(
        '1024x768',
        '1600x1200',
        '2048x1536'
    ),
    '16:9' => array(
        '1280x720',
        '1366x768',
        '1600x900',
        '1920x1080',
        '2560x1440'
    ),
    '16:10' => array(
        '1280x800',
        '1440x900',
        '1680x1050',
        '1920x1200',
        '2560x1600'
    )
);

How to get 'aspect ratio', ie values: 4:3, 16:9, 16:10?

echo $resolutions[0]

doesn't output anything

Upvotes: 0

Views: 51

Answers (2)

rr-
rr-

Reputation: 14811

This is an associative array.

You can access its values like this:

echo $resolutions['4:3'];

To get list of its keys, you can use array_keys:

$keys = array_keys($resolutions);
print_r($keys); //4:3, 16:9, 16:10

Finally, in order to iterate through all of its keys, you can do following:

foreach ($resolutions as $aspectRatio => $resolutions)
{
     echo 'Resolution ' . $aspectRatio . ': ' . PHP_EOL;
     print_r($resolutions);
}

Upvotes: 1

bitWorking
bitWorking

Reputation: 12655

$keys = array_keys($resolutions);
echo $keys[0];

Upvotes: 0

Related Questions