views:

71

answers:

3

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

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;

Any ideas ?

+6  A: 

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.

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

Ondrej Slinták
These are the sort of arrays you get back from a SOAP call. Its the XML structure expressed as a set fo recursive hashes.
James Anderson
+1  A: 

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:

$data = (object) $myarray;

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

animuson
Ok understood , the reason I was treating it like a object is because I was using SOAPCLIENT which for some reason returned a std class . I have recently swapped servers and had to implement NUSOAP instead
Joe appleton
+1  A: 

Use like this,

array stored in some variable called as $res

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