2012-04-05 44 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