質問

率直に言っておきます:私は PHP ハックです。おそらくここには愚かな間違いがあるでしょう。見かけたら指摘してください。

私がやろうとしていること: Yelp のレビューを表示したいレストランのページを作成しています。Yelp Phone API を使用して、特定のビジネスのレビューを取得しています。ここにある Yelp API ドキュメントのサンプル応答を参照してください。 http://www.yelp.com/developers/documentation/phone_api#sampleResponse

私がやったこと:

  • API に正常に接続され、応答が返されました。
  • foreach ループ内の応答配列からエコーされた値。

ドキュメントを見ると、応答にはいくつかのレベルがあることがわかります。2 番目の層の値を簡単に出力したりエコーしたりできますが、私が本当に求めているものはすべて、応答の「reviews」セクションにネストされています。レビューセクション内の値(user_name、review_excerptなど)をエコーする方法がわかりません。

私のコード:

$yelpstring = file_get_contents("http://api.yelp.com/phone_search?phone=[redactedphonenumber]&ywsid=[redactedapikey]", true);
$obj = json_decode($yelpstring);

foreach($obj->businesses as $key => $business)
{
$reviews = $business->reviews;

//print_r($reviews);
echo $reviews['user_name'];
}

$reviews をエコーすると、「Array」という単語が表示されるだけです。print_r($reviews) を実行すると、期待されるキーと値のリストが得られます。array(echo $reviews['user_name']) から特定の値をエコーし​​ようとしても、何も得られません。私が間違っていることに光を当てていただければ幸いです。単純な何かが欠けていると確信しています。お時間をいただきありがとうございました!

編集:print_r($reviews) の出力:

Array ( [0] => stdClass Object ( [rating_img_url_small] => http://media4.px.yelpcdn.com/static/201012164278297776/img/ico/stars/stars_small_2.png [user_photo_url_small] => http://media2.px.yelpcdn.com/static/201012162819681786/img/gfx/blank_user_extra_small.gif [rating_img_url] => http://media4.px.yelpcdn.com/static/201012163489049252/img/ico/stars/stars_2.png [rating] => 2 [user_url] => http://www.yelp.com/user_details?userid=vZbcPrYPSMFIDIfTub5H1g [url] => http://www.yelp.com/biz/jelly-cafe-denver#hrid:u9ckRV6tKApe6Bu93M93CA [mobile_uri] => http://m.yelp.com/biz/5G2X2q9p7QFdm-LbyutltQ?srid=u9ckRV6tKApe6Bu93M93CA [text_excerpt] => I wanted to like this place. It's got the contemporary name and it's full of hipsters. The place looked clean and the style was fun and cute. I felt like... [user_photo_url] => http://media3.px.yelpcdn.com/static/201012161186834854/img/gfx/blank_user_small.gif [date] => 2011-09-07 [user_name] => boycott p. [id] => u9ckRV6tKApe6Bu93M93CA ) [1] => stdClass Object ( [rating_img_url_small] => http://media4.px.yelpcdn.com/static/201012164278297776/img/ico/stars/stars_small_2.png [user_photo_url_small] => http://media1.px.yelpcdn.com/upthumb/MWu84G5QtmBmT9GoqjT_kg/ss [rating_img_url] => http://media4.px.yelpcdn.com/static/201012163489049252/img/ico/stars/stars_2.png [rating] => 2 [user_url] => http://www.yelp.com/user_details?userid=izF2cGrmqt-u_Z2tDZ8dbg [url] => http://www.yelp.com/biz/jelly-cafe-denver#hrid:OYLeeCMgnpZkk1c9LWu97g [mobile_uri] => http://m.yelp.com/biz/5G2X2q9p7QFdm-LbyutltQ?srid=OYLeeCMgnpZkk1c9LWu97g [text_excerpt] => Food is decent and overpriced, but service is a joke. Your food will take a minimum of 20 minutes, for the basic breakfast. Then when your food does come... [user_photo_url] => http://media1.px.yelpcdn.com/upthumb/MWu84G5QtmBmT9GoqjT_kg/ms [date] => 2011-09-06 [user_name] => April H. [id] => OYLeeCMgnpZkk1c9LWu97g ) [2] => stdClass Object ( [rating_img_url_small] => http://media2.px.yelpcdn.com/static/20101216418129184/img/ico/stars/stars_small_4.png [user_photo_url_small] => http://media1.px.yelpcdn.com/upthumb/3euzdGdLZRFxImY68MSg7w/ss [rating_img_url] => http://media2.px.yelpcdn.com/static/201012164084228337/img/ico/stars/stars_4.png [rating] => 4 [user_url] => http://www.yelp.com/user_details?userid=bHR9UU4vtx2QKZD44O0E5g [url] => http://www.yelp.com/biz/jelly-cafe-denver#hrid:njvNAzfSII3PxXyUymLZ1w [mobile_uri] => http://m.yelp.com/biz/5G2X2q9p7QFdm-LbyutltQ?srid=njvNAzfSII3PxXyUymLZ1w [text_excerpt] => Stopped here for breakfast on a friday morning. We were seated immediately and had a really friendly waitress. I ordered a side order of the Chai french... [user_photo_url] => http://media1.px.yelpcdn.com/upthumb/3euzdGdLZRFxImY68MSg7w/ms [date] => 2011-09-05 [user_name] => Diane F. [id] => njvNAzfSII3PxXyUymLZ1w ) ) 
役に立ちましたか?

解決

print_r の出力によると、$reviews['user_name'] を参照することはできません。

$reviews はオブジェクトの配列であることに注意してください。したがって、 user_name にアクセスするには、使用する必要があります

echo $reviews[0]->user_name;

配列内に複数の項目がある場合は、次のようなループが必要になります。

for ($i = 0; $i<count($reviews); $i++) {
    echo $reviews[$i]->user_name;
}

これがお役に立てば幸いです。

他のヒント

$reviews レビューオブジェクトの配列です。目的のデータに到達するには、ループする必要があります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top