2016-04-18 198 views
1

在db:如何訪問到嵌套對象

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

使用打印後:

Array (
    [49530fe2e872288d92042b3059f31566] => Array (
     [filename] => 49530fe2e872288d92042b3059f31566.jpg 
    ) 
    [4b7dc54328383c294ceb884e9691838c] => Array (
     [filename] => 4b7dc54328383c294ceb884e9691838c.jpg 
    ) 
) 

如何我可以訪問filename

+1

您是否試圖做到這一點? – Mattia

回答

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>'; 
    } 
} 
?> 
+0

謝謝你的回答:) –

+0

@raafetdhaouadi,不客氣。 – Pupil

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 
}