0

私のウェブサイトの近くのリストをランダムに表示するために、yelp API を使用しています。ウェブサイトの yelp のドキュメントから:

rating                 number   Rating for this business (value ranges from 1, 1.5, ... 4.5, 5)
rating_img_url         string   URL to star rating image for this business (size = 84x17)
rating_img_url_small   string   URL to small version of rating image for this business (size = 50x10)
rating_img_url_large   string   URL to large version of rating image for this business (size = 166x30)
snippet_text           string   Snippet text associated with this business
snippet_image_url      string   URL of snippet image associated with this business
location               dict     Location data for this business
location.address       list     Address for this business. Only includes address fields.
location.display_address list   Address for this business formatted for display. Includes all address fields, cross streets and city, state_code, etc.
location.city          string   City for this business
location.state_code    string   ISO 3166-2 state code for this business
location.postal_code   string   Postal code for this business
location.country_code  string   ISO 3166-1 country code for this business
location.cross_streets string   Cross streets for this business

location.display_address などの位置変数を出力するにはどうすればよいですか? 以下の私のコードは、「$response->businesses->rating」をエコーすることにより、文字列と数字を正しく出力します。ただし、コードの最後の行が機能しません。

// Handle Yelp response data
$response = json_decode($data);
$business = $response->businesses;

$numbers = range(0, 19);
shuffle($numbers);
echo "<img src='".$business[$numbers[$ran]]->image_url."'><br/>";
echo $business[$numbers[$ran]]->name."<br/>";
echo "<img border=0 src='".$business[$numbers[$ran]]->rating_img_url_large."'><br/>";
echo "<br/>";

echo $business[$numbers[$ran]]->location[display_address]."<br/>";

私が得るエラーは

「致命的なエラー: 51 行目の /home/content/38/11397138/html/yelpcall.php で stdClass 型のオブジェクトを配列として使用できません」

編集: var ダンプの戻り値:

object(stdClass)#14 (6) { ["city"]=> string(11) "Chino Hills" ["display_address"]=> array(2) { [0]=> string(14) "2923 Chino Ave" [1]=> string(21) "Chino Hills, CA 91709" } ["postal_code"]=> string(5) "91709" ["country_code"]=> string(2) "US" ["address"]=> array(1) { [0]=> string(14) "2923 Chino Ave" } ["state_code"]=> string(2) "CA" }
4

2 に答える 2

0

これを試してみるvar_dump($business[$numbers[$ran]]->location);と、使用している変数について詳しくわかり、そのプロパティを公開するのに役立ちます。

あなたの場合、 location プロパティのタイプは StdClass のようです。したがって、次のようにプロパティにアクセスします。$business[$numbers[$ran]]->location->display_address

追記:更新ありがとうございます。StdClass の display_address プロパティは数値インデックス配列であるため、配列の要素にアクセスするには、 $business[$numbers[$ran]]->location->display_address[0] 次のようにします。

$address = $business[$numbers[$ran]]->location->display_address;
foreach( $address as $line ){
    echo $line.'<br />';
} 
于 2013-07-22T07:46:17.513 に答える
0

echo $business[$numbers[$ran]]->location["display_address"]."
";を試してください。(「display_address」キー名を引用符で囲みます。

于 2013-07-22T07:46:23.163 に答える