如何在PHP中从复杂数组中获取值?

时间:2021-12-06 12:16:53

I am calling a soap function that returns the following array:

我正在调用一个返回以下数组的soap函数:

Array ( [FastAddressResult] => Array ( [IsError] => false [ErrorNumber] => 0 [ErrorMessage] => [Results] => Array ( [Address] => Array ( [Id] => 13872147.00 [OrganisationName] => [DepartmentName] => [Line1] => Methley Grove [Line2] => [Line3] => [Line4] => [Line5] => [PostTown] => Leeds [County] => West Yorkshire [Postcode] => LS7 3PA [Mailsort] => 64121 [Barcode] => [IsResidential] => false [IsSmallOrganisation] => false [IsLargeOrganisation] => false [RawData] => [GeographicData] => Array ( [GridEastM] => 0 [GridNorthM] => 0 [Objective2] => false [Transitional] => false [Longitude] => 0 [Latitude] => 0 [WGS84Longitude] => 0 [WGS84Latitude] => 0 ) ) ) )

数组([FastAddressResult] =>数组([IsError] => false [ErrorNumber] => 0 [ErrorMessage] => [结果] =>数组([地址] =>数组([Id] => 13872147.00 [OrganisationName] = > [DepartmentName] => [Line1] => Methley Grove [Line2] => [Line3] => [Line4] => [Line5] => [PostTown] =>利兹[县] =>西约克郡[邮编] = > LS7 3PA [Mailsort] => 64121 [Barcode] => [IsResidential] => false [IsSmallOrganisation] => false [IsLargeOrganisation] => false [RawData] => [GeographicData] =>数组([GridEastM] => 0 [GridNorthM] => 0 [Objective2] => false [Transitional] => false [经度] => 0 [纬度] => 0 [WGS84Longitude] => 0 [WGS84Latitude] => 0))))

I need to exstract the values the following does not seem to work:

我需要提取以下似乎不起作用的值:

$this->adressline1 = $result->FastAddressResult->Results->Address->Line1;

3 个解决方案

#1


6  

Try:

尝试:

$this->adressline1 = $result[ "FastAddressResult" ][ "Results" ][ "Address" ][ "Line1" ];

You'd want to use $result->fastAddressResult->etc if $result was an object. Check this page for more info about PHP arrays.

如果$ result是一个对象,你想使用$ result-> fastAddressResult-> etc。有关PHP数组的更多信息,请查看此页面。

Also, such complex array has certain design smells. You could think of a way to make it simpler and more readable.

而且,这种复杂阵列具有某些设计气味。您可以想出一种方法,使其更简单,更易读。

#2


1  

Obviously someone didn't pay attention in programming class when they covered Arrays...

显然有人在编程类中没有注意它们覆盖数组时...

$this->adressline1 = $result['FastAddressResult']['Results']['Address']['Line1'];

or convert the array to an stdClass first:

或者首先将数组转换为stdClass:

$data = (object) $myarray;

but you'd have to do that for all the arrays in that too, so no.

但是你也必须为所有阵列做到这一点,所以没有。

#3


1  

Use like this,

像这样使用,

array stored in some variable called as $res

数组存储在一个名为$ res的变量中

   echo $res[ "FastAddressResult" ][ "Results" ][ "Address" ][ "Line1" ];

#1


6  

Try:

尝试:

$this->adressline1 = $result[ "FastAddressResult" ][ "Results" ][ "Address" ][ "Line1" ];

You'd want to use $result->fastAddressResult->etc if $result was an object. Check this page for more info about PHP arrays.

如果$ result是一个对象,你想使用$ result-> fastAddressResult-> etc。有关PHP数组的更多信息,请查看此页面。

Also, such complex array has certain design smells. You could think of a way to make it simpler and more readable.

而且,这种复杂阵列具有某些设计气味。您可以想出一种方法,使其更简单,更易读。

#2


1  

Obviously someone didn't pay attention in programming class when they covered Arrays...

显然有人在编程类中没有注意它们覆盖数组时...

$this->adressline1 = $result['FastAddressResult']['Results']['Address']['Line1'];

or convert the array to an stdClass first:

或者首先将数组转换为stdClass:

$data = (object) $myarray;

but you'd have to do that for all the arrays in that too, so no.

但是你也必须为所有阵列做到这一点,所以没有。

#3


1  

Use like this,

像这样使用,

array stored in some variable called as $res

数组存储在一个名为$ res的变量中

   echo $res[ "FastAddressResult" ][ "Results" ][ "Address" ][ "Line1" ];