0

サブアレイの出力に問題があります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'] のように試してみました

4

1 に答える 1

2

これの代わりに:

return json_encode($test);

これを行うだけです:

return $test;

まだphpで作業しているため、json_encodeする必要はありません。次に、 index.php でこれを行うことができます:

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

最初の画像を表示するだけでなく、結果をループすることも必要になると思いますが、それについて助けが必要だとは言わなかったので、ここで最初の結果をエコーし​​ます。

于 2012-04-05T21:46:25.363 に答える