我使用聯邦快遞的API爲他們的商店查找「dropoff」位置,然後我將使用地圖API(Google)顯示。面向對象的PHP數組 - 從OO創建「位置」列表PHP數據
該API正在工作,但我有麻煩,因爲我不熟悉面向對象的數組。
我想將數組中的值存儲爲唯一變量,因此我可以將它們傳遞給我的地圖API。
我試圖完成類似下面:
<?php
// MY "IDEAL" solution - any other ideas welcome
// (yes, reading up on Object Oriented PHP is on the to-do list...)
$response = $client ->fedExLocator($request);
if ($response -> HighestSeverity != 'FAILURE' && $response -> HighestSeverity != 'ERROR')
{
$response -> BusinessAddress -> StreetLines[0] = $location_0;
$response -> BusinessAddress -> StreetLines[1] = $location_1;
$response -> BusinessAddress -> StreetLines[2] = $location_2;
}
?>
工作聯邦快遞代碼示例:
<?php
$response = $client ->fedExLocator($request);
if ($response -> HighestSeverity != 'FAILURE' && $response -> HighestSeverity != 'ERROR')
{
echo 'Dropoff Locations<br>';
echo '<table border="1"><tr><td>Streetline</td><td>City</td><td>State</td><td>Postal Code</td><td>Distance</td></tr>';
foreach ($response -> DropoffLocations as $location)
{
if(is_array($response -> DropoffLocations))
{
echo '<tr>';
echo '<td>'.$location -> BusinessAddress -> StreetLines. '</td>';
echo '<td>'.$location -> BusinessAddress -> PostalCode. '</td>';
echo '</tr>';
}
else
{
echo $location . Newline;
}
}
echo '</table>';
}
?>
你想要將位置存儲到哪個數組?你也可以打印一個典型的'$ response'對象的var_dump嗎? – 2012-08-14 21:13:36
你爲什麼要分配給從Fedex收到的'$ response'數組?這是你的數據*來源*。您應該將這些值分配給您自己的數據對象。 – 2012-08-14 21:14:38
試試'$ response-> DropOffLocations [0] - > BusinessAdress-> StreetLines [0]'而不是'$ response-> BusinessAdress-> StreetLines [0]'。 – jeremy 2012-08-14 21:15:00