I’m using FedEx’s API to find “dropoff” locations for their stores that I then will be displaying using a map API (Google).
The API is working however I am having trouble as I am unfamiliar with the Object Oriented array.
I’d like to store values in the array as unique variables so I can pass them to my map API.
I’m trying to accomplish something LIKE the below:
<?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;
}
?>
Working FedEx Code Sample:
<?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>';
}
?>
OK, from what I can tell, the
$responseobject has two members:$response->HighestSeverity, which is a string, and$response->DropoffLocations, which is an array.$response->DropoffLocationsis just an array, nothing fancy about it on its face. You can refer to its entries with square brackets (e.g.$response->DropoffLocations[0]etc), or, as they have done, walk through it withforeach.The only thing “object oriented” about the array, other than the fact that it is a member of an object, is that its entries are objects, rather than simple values.
As a result, you’re putting your indexing in the wrong place (and missing
DropoffLocationsaltogether). Instead of, for example, this:You should be indexing
$response->DropoffLocationsitself, and then pulling the member variables from each entry, like this:Do note @PeterGluck’s comment, though. It’s very unlikely that you want to set that value to anything.