我在打印子阵列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-&gt; homemain ['image']
答案 0 :(得分:2)
而不是:
return json_encode($test);
这样做:
return $test;
你不需要json_encode,因为你还在php中使用它。然后在index.php中你可以这样做:
$banner = $fetch->main("hits");
echo $banner[0]['homemain']['image'];
我假设您还想循环显示结果,而不是只显示第一个图像,但您没有说您需要帮助,所以我只是在这里回应第一个结果。