php - 如何从多维数组中获取此值

时间:2013-06-30 17:01:37

标签: php arrays multidimensional-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'
    )
);

如何获得'宽高比',即值:4:3,16:9,16:10?

  

echo $ resolutions [0]

不输出任何内容

2 个答案:

答案 0 :(得分:1)

这是associative array

您可以像这样访问其值:

echo $resolutions['4:3'];

要获取其密钥列表,您可以使用array_keys

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

最后,为了遍历其所有键,您可以执行以下操作:

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

答案 1 :(得分:0)

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