Forum Moderators: coopster

Message Too Old, No Replies

Web service respose into PHP variables

getting data from an XML array

         

Drunk N Japan

4:12 pm on May 9, 2007 (gmt 0)

10+ Year Member



I have been working with a address verification web service, and I am getting a results array, the problem I am having is pulling the data from the results array into PHP variables that I can pass back to the main program. I would appreciate any help I can Get. I have tried every suggestion that I can find in the forums and still cannot figure this out. This is the first time I have used nusoap and XML. BTW I am running PHP4.3.1 so I cannot use the simple XML functions of PHP5. :( Here is the response array

Array
(
[CleanResult] => Array
(
[OriginalAddress] => Array
(
[OriginalStreet] => 123 maindrag
[OriginalUnit] =>
[OriginalCity] => anytwon
[OriginalState] => AL
[OriginalZip] => 12345
[RequestType] => VERIFY
)

[CleanAddress] => Array
(
[AddressFinal] => 123 MAIN DRAG DR SE
[AddressDelimited] => 123¦¦MAIN DRAG¦DR¦SE¦¦
[AddressLeftovers] =>
[CityFinal] => ANYTOWN
[CityPreferred] => ANYTOWN
[CityStateZipFinal] => ANYTOWN AL 12345-1234
[CityStateZipPreferred] => ANYTOWN AL 12345-1234
[CountyName] => COUNTY
[Quality] => SUFFIX ADDED
[StateFinal] => AL
[ZIPFiveDigit] => 12345
[ZIPFourDigitAddOn] => 1234
[ZIPFinal] => 12345-1234
)

)

)

this is the part of the PHP script that I am using to pull the data into PHP variables.

//get the array contents
$myar=$results;
$myar_sub = $myar[CleanResult];

for ($i = 0; $i < count($myar_sub); $i++) {

// Trying to put the XML data into variables

$Address = $myar_sub[$i]['AddressFinal'];
$AddressDelimited = $myar_sub[$i]['AddressDelimited'];
$Address_2 = $myar_sub[$i]['AddressLeftovers'];
$CityFinal = $myar_sub[$i]['CityFinal'];
$City = $myar_sub[$i]['CityPreferred'];
$CityStateZipFinal = $myar_sub[$i]['CityStateZipFinal'];
$CityStateZipPreferred = $myar_sub[$i]['CityStateZipPreferred'];
$CountyName = $myar_sub[$i]['CountyName'];
$Quality = $myar_sub[$i]['Quality'];
$State = $myar_sub[$i]['StateFinal'];
$Zip_Code = $myar_sub[$i]['ZIPFiveDigit'];
$Zip_Code2 = $myar_sub[$i]['ZIPFourDigitAddOn'];
$ZIPFinal = $myar_sub[$i]['ZIPFinal'];

echo $Address; //debugger echo
}

The code part is where I am having the most difficulty. Thanks for any help you can give.

Drunk N Japan

6:19 pm on May 9, 2007 (gmt 0)

10+ Year Member



I have also put
echo count($myar);
after the
$myar = $results.
I am getting a count = 0.

HELP!
I need somebody.
HELP!
Not just anybody.
HELP!
You know I need Someone to HEeeeellllp!

Sorry, just stressing over the whole issue.

Drunk N Japan

8:52 pm on May 9, 2007 (gmt 0)

10+ Year Member



Well if you take the s of the end of result you can actually read the array. Next there is no need for a for loop. Just put the values directly into the variables you want. All the values have a key so it is quite simple. Here is what your code should look like.


// Get the file contents
$myar=$result[CleanResult][CleanAddress];

//put the contents into the proper variable
$Address = $result[CleanResult][CleanAddress][AddressFinal];
$AddressDelimited =$result[CleanResult][CleanAddress][AddressDelimited];
$Address_2 = $result[CleanResult][CleanAddress][AddressLeftovers];
$CityFinal = $result[CleanResult][CleanAddress][CityFinal];
$City = $result[CleanResult][CleanAddress][CityPreferred];
$CityStateZipFinal = $result[CleanResult][CleanAddress][CityStateZipFinal];
$CityStateZipPreferred = $result[CleanResult][CleanAddress][CityStateZipPreferred];
$CountyName = $result[CleanResult][CleanAddress][CountyName];
$Quality = $result[CleanResult][CleanAddress][Quality];
$State = $result[CleanResult][CleanAddress][StateFinal];
$Zip_Code = $result[CleanResult][CleanAddress][ZIPFiveDigit];
$Zip_Code2 = $result[CleanResult][CleanAddress][ZIPFourDigitAddOn];
$ZIPFinal = $result[CleanResult][CleanAddress][ZIPFinal];

That there code looks very nice if I do say so my self. I hope that this can help someone else in the future.

It's "Happy Hour" somewhere.
Drunk

eelixduppy

8:58 pm on May 9, 2007 (gmt 0)



Glad you figured it out! Thanks for sharing your solution :)

Drunk N Japan

12:31 pm on May 10, 2007 (gmt 0)

10+ Year Member



Sometimes you have to get a different perspective to figure this stuff out. I love working with php as I can do thing in it that I cannot in other languages. If I find any other answers to this problem I will post them here.

T-minus 8 hours till "Happy Hour"