2012-04-05 23 views
0
我有一些問題,打印出來的子陣

homemain=>imageMongoDB的打印圖像子陣

這是我的JSON

{ 
    "_id": ObjectId("4f7d0b9638fc5dc54c000000"), 
    "station": "hits", 
    "homemain": { 
    "image": "URL", 
    "link": "URL LINK" 
    } 
} 

正如你可以看到在homemain它有像我想的話網址是打印在頁面上。

這是我的PHP腳本

public function main($stationname) 
    { 
     // select a collection (analogous to a relational database's table) 
     $collection = $this->db->Stations_Banner; 

     // find everything in the collection 
     $cursor = $collection->find(array("station"=>"hits"),array("homemain")); 

     $test = array(); 
      // iterate through the results 
      while($cursor->hasNext()) { 
       $test[] = ($cursor->getNext()); 
      } 
     //Print Results 
     return json_encode($test); 

    } 

那麼我的index.php頁面上我把這個

<?php 
    $banner = $fetch->main("hits"); 
    echo $banner;  
?> 

我曾嘗試使用$banner->homemain->image我也試着像$ banner-> homemain [「形象」]

如果有人可以幫助將是巨大的。

+0

你的回聲當前輸出了什麼? – 2012-04-05 21:40:27

+0

json string [{「_id」:{「$ id」:「4f7d0b9638fc5dc54c000000」},「homemain」:{「image」:「URL」,「link」:「URL LINK」}}] – RussellHarrower 2012-04-05 21:41:10

回答

2

取而代之的是:

return json_encode($test); 

只是這樣做:

return $test; 

你並不需要json_encode因爲你還在使用它在PHP。然後在你的index.php,你可以只是這樣做:

$banner = $fetch->main("hits"); 
echo $banner[0]['homemain']['image']; 

我假設你還會通過的結果,而不是僅僅顯示了第一個圖像要循環,但你沒有說你需要幫助,所以我只是在這裏迴應第一個結果。

+0

Thank you that worked a魅力。我想循環它我會使用一個foreach或一段時間它看起來更像是一個while循環,因爲它看起來像MySQL會給我做的事情,當我做一個查詢 – RussellHarrower 2012-04-05 21:51:44