2014-02-06 27 views
2

我已經發布的這部分..但這個它如何訪問JSON子陣列 - PHP

我有以下

foreach ($results['comments'] as $item) { 

    echo 'Date: '. $item['created_at'] .'<br/>'; 
    echo 'Description : '. $item['html_body'] .'<br/>'; 
    echo 'Attachments : '. $item['attacments->url'] .'<br/>'; 
    echo 'Filename : '. $item['file_name'] .'<br/>'; 
    echo "<br>"; 
} 

所以基本上,我的日期和說明工作不同的問題,但附件不會工作,b/ci不認爲這是正確的方式來獲得一個數組內的數組對象嗎?希望我解釋正確。

comments數組具有所有日期作爲單個對象,所以是描述,那麼它有這個尾隨。

[public] => 1 [trusted] => 1 [attachments] => Array ([0] => Array ([url] => https://url/api/v2/attachments/IDHERE.json [id] => ID#[file_name] => name of file here 
+0

嘗試'$項目[ '附件'] [0] [ '網址'] ' –

+0

如何file_name?它會是$ item ['attachments'] [0] ['url'] [0] ['id'] [0] ['file_name'] @ Nouphal.M – user3100345

回答

3

看看你的陣列傾倒

[public] => 1 
[trusted] => 1 
[attachments] => Array (
    [0] => Array (
     [url] => https://url/api/v2/attachments/IDHERE.json 
     [id] => ID# 
     [file_name] => name of file here 

得到這樣的價值觀:

$Attachments = $item['attachments']; 
$AttachmentsUrl = $Attachments[0]['url']; 
$Attachmentsid = $Attachments[0]['id']; 
$AttachmentsFileName = $Attachments[0]['file_name']; 
+0

工作! @meda – user3100345

+0

我可以問,因爲代碼給了我一個錯誤的PHP注意:未定義的偏移量:0,我不能使用數組的名稱嗎?如果它看起來@meda – user3100345

+0

@ user3100345如果它拋出一個錯誤,這意味着'$附件[0]'沒有任何元素在該索引 – meda