我在打印子数组homemain=>image时遇到一些问题
这是我的JSON
{
"_id": ObjectId("4f7d0b9638fc5dc54c000000"),
"station": "hits",
"homemain": {
"image": "URL",
"link": "URL LINK"
}
}正如你可以在homemain下看到的,它有图像,我想要在页面上打印出单词URL。
这是我的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‘’image‘
发布于 2012-04-06 05:46:25
而不是这样:
return json_encode($test);只需执行以下操作:
return $test;你不需要json_encode它,因为你仍然在php中使用它。然后在你的index.php中,你可以这样做:
$banner = $fetch->main("hits");
echo $banner[0]['homemain']['image'];我假设您还想循环遍历结果,而不是只显示第一个结果的图像,但您并没有说需要帮助,所以我在这里只是呼应第一个结果。
https://stackoverflow.com/questions/10036295
复制相似问题