如何访问嵌套对象

时间:2016-04-18 08:25:47

标签: php arrays json object

在db:

window.notify()

使用打印后:

{"49530fe2e872288d92042b3059f31566":{"filename":"49530fe2e872288d92042b3059f31566.jpg"},"4b7dc54328383c294ceb884e9691838c":{"filename":"4b7dc54328383c294ceb884e9691838c.jpg"}}

如何访问Array ( [49530fe2e872288d92042b3059f31566] => Array ( [filename] => 49530fe2e872288d92042b3059f31566.jpg ) [4b7dc54328383c294ceb884e9691838c] => Array ( [filename] => 4b7dc54328383c294ceb884e9691838c.jpg ) )

2 个答案:

答案 0 :(得分:0)

在数组上使用foreach循环来获取filename

<?php
$j = '{"49530fe2e872288d92042b3059f31566":{"filename":"49530fe2e872288d92042b3059f31566.jpg"},"4b7dc54328383c294ceb884e9691838c":{"filename":"4b7dc54328383c294ceb884e9691838c.jpg"}}';
$a = json_decode($j, TRUE);
if (! empty($a)) {
    foreach ($a as $e) {
        echo '<pre>';print_r($e['filename']);echo '</pre>';
    }
}
?>

答案 1 :(得分:0)

如果您不知道数组的键,请使用foreach循环。

$arr = array (
    '49530fe2e872288d92042b3059f31566' => array (
        'filename' => '49530fe2e872288d92042b3059f31566.jpg'
    ),
    '4b7dc54328383c294ceb884e9691838c' => array (
        'filename' => '4b7dc54328383c294ceb884e9691838c.jpg'
    )
); //To obtain this array from json , do **$arr = json_decode($json, true);**
foreach($arr as $val){
    $filename  = $val['filename']; //There you go
}