简体   繁体   English

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

[英]How can I get values out of a complex array in PHP?

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 ) ) ) )

I need to exstract the values the following does not seem to work: 我需要提取以下似乎无效的值:

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

Try: 尝试:

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

You'd want to use $result->fastAddressResult->etc if $result was an object. 如果$ result是对象,则要使用$ result-> fastAddressResult-> etc。 Check this page for more info about PHP arrays. 检查此页面以获取有关PHP数组的更多信息。

Also, such complex array has certain design smells. 而且,这种复杂的阵列具有一定的设计气味。 You could think of a way to make it simpler and more readable. 您可能会想到一种使其更简单易读的方法。

Obviously someone didn't pay attention in programming class when they covered Arrays... 显然,当有人学习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. 但是您也必须对其中的所有数组执行此操作,所以不行。

Use like this, 这样使用

array stored in some variable called as $res 数组存储在称为$ res的某个变量中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM