XML processing issues with PHP SimpleXML

http://pastie.org/1701923 Here, the XML is returned from the API that I am requesting for zip codes.

I want to pull data from each record and either program it directly or put into an array that I can execute. I don't seem to understand. Here is the last code I used:

$xml = new SimpleXMLElement($results);
foreach($xml->zipcoderadius->zipcodes as $loc) {
    $codes[] = (string)$loc['zipcode'];
}
print_r($codes);
die();

($ results - returned XML from CURL)

What is output is Array ([0] => [1] => [2] => [3] => [4] =>)

+3
source share
2 answers

I think it was, n is a string (zipcode). Try no line

0
source

, . , , cURL $xmlStr .

$push = array();
$foo = simplexml_load_string($xmlStr);
foreach($foo->zipcoderadius->zipcodes->id as $bar) {
    array_push($push, (string)$bar);
}
print_r($push);

:

Array
(
    [0] => 75969
    [1] => 75970
    [2] => 75971
)

: SimpleXML :

object(SimpleXMLElement)#1 (1) {
  ["zipcoderadius"]=>
  object(SimpleXMLElement)#4 (1) {
    ["zipcodes"]=>
    object(SimpleXMLElement)#3 (32) {
      ["id"]=>
      array(3) {
        [0]=>
        string(5) "75969"
        [1]=>
        string(5) "75970"
        [2]=>
        string(5) "75971"
      }
      ["zipcode"]=>
      array(3) {
        [0]=>
        string(5) "94945"
        [1]=>
        string(5) "94945"
        [2]=>
        string(5) "94945"
      }
...
0

All Articles