我有一種情況,我可以在這種情況下從Google地理編碼api或地方api中收回兩個json對象之一。從地理編碼API它看起來像這樣php遍歷使用varriables屬性名稱的對象
Acccessing值:
$coordinates = $data->results[0]->geometry->location;
$cache_value['lat'] = (string) $coordinates->lat;
$cache_value['lng'] = (string) $coordinates->lng;
和地方的結構幾乎一致。
$coordinates = $data->result->geometry->location;
$cache_value['lat'] = (string) $coordinates->lat;
$cache_value['lng'] = (string) $coordinates->lng;
在我的代碼我有兩個函數來處理每個案件,但它們與result
VS results[0]
之外幾乎idential,我想將它們結合起來。我試圖傳遞一個varriable但它拋出錯誤:
$result = ($place) ? 'result' : 'results[0]';
$coordinates = $data->$result->geometry->location;
提供了以下:
注意: Undefined property: stdClass::$result[0]
我想知道正確的語法才達到什麼即時通訊後, ,以及任何關於命名的指針,因爲我擔心這個問題的標題有點不合時宜。
是否缺少''results [0];'錯字? –
爲什麼不只是 $ result =($ place)? $ data-> result:$ data-> results [0];然後$ coordinates = $ result-> geometry-> location; – stakolee
@RJJordan,現在已經得到糾正 - 謝謝 – orionrush