我正在使用FedEx的API查找其商店的“下线”位置,然后我将使用地图API(Google)显示这些位置。
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;
}
?>
使用FedEx代码示例:
<?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>';
}
?>
答案 0 :(得分:1)
好的,据我所知,$response
对象有两个成员:$response->HighestSeverity
,一个字符串,$response->DropoffLocations
,它是一个数组。 $response->DropoffLocations
只是一个数组,它的脸上没什么好看的。您可以使用方括号引用其条目(例如$response->DropoffLocations[0]
等),或者,正如他们所做的那样,使用foreach
遍历它。
关于数组的唯一“面向对象”,除了它是一个对象的成员之外,它的条目是对象,而不是简单的值。
因此,您将索引放在错误的位置(并且完全丢失DropoffLocations
)。而不是,例如,这个:
$response -> BusinessAddress -> StreetLines[0] = $location_0;
您应该将$response->DropoffLocations
本身编入索引,然后从每个条目中提取成员变量,如下所示:
$response -> DropoffLocations[0] -> BusinessAddress -> StreetLines = $location_0;
请注意@ PeterGluck的评论。您不太可能将该值设置为任何值。