2012-08-14 55 views
0

我使用联邦快递的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>'; 
} 

?> 
+1

你想要将位置存储到哪个数组?你也可以打印一个典型的'$ response'对象的var_dump吗? – 2012-08-14 21:13:36

+1

你为什么要分配给从Fedex收到的'$ response'数组?这是你的数据*来源*。您应该将这些值分配给您自己的数据对象。 – 2012-08-14 21:14:38

+0

试试'$ response-> DropOffLocations [0] - > BusinessAdress-> StreetLines [0]'而不是'$ response-> BusinessAdress-> StreetLines [0]'。 – jeremy 2012-08-14 21:15:00

回答

1

OK,从我所知道的,$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的评论,虽然。这是不太可能的,你想设置该值任何东西。

+1

不是'$ response-> DropOffLocations [0] - > BusinessAdress-> StreetLines' ,因为'foreach'不是来自'$ response',而是来自'$ response-> DropOffLocations'? – jeremy 2012-08-14 21:17:30

+0

'$ response'不是一个数组。这是一个清楚的对象。 – 2012-08-14 21:18:02

+0

@Nile:嗯,刚刚注意到,修复了。 – KRyan 2012-08-14 21:19:29