Php array question

Hai folks,

array(1) { [0]=> object(SimpleXMLElement)#110 (1) { [0]=> string(4) “1757” } }

i just want to get above array value 1757 to a variable named $zip.

pls help:rolleyes::rolleyes:

It seems that you are parsing XML with SimpleXML library. So need to know how you have parsing it. Can you paste your code here?

Hai rajung,

for ($x=1;$x<=$totfound;$x++){
 	$result = $xml->xpath('//wp:listing['.$x.']/wp:address/wp:zip4');
	$t=count($result);
	if ($t==1){
	   var_dump($result); // this is the array sample shown in my first post.
	   die;
	}
...

xml
what i am trying to do is, get all the zip4 tag values to an array.

-<wp:listing>
–<wp:address>
—<wp:zip4=“1757”>
—</wp:zip4>
–</wp:address>
-</wp:listing>

-<wp:listing>
–<wp:address>
—<wp:zip>
—</wp:zip=“47814565”>
–</wp:address>
-</wp:listing>

-<wp:listing>
–<wp:address>
—<wp:zip4=“1350”>
—</wp:zip4>
–</wp:address>
-</wp:listing>

  • some wp:listing does not have the zip4 tag.

Did you try just an echo?


echo (string) $result;

Hai rajug,

for ($x=1;$x<=$totfound;$x++){
 	$result = $xml->xpath('//wp:listing['.$x.']/wp:address/wp:zip4');
	echo (string)$result;
	die;

it says : Array :slight_smile:

This works for currently.
but i just doubted weather it appropriate to use for each on this occation since i certenly know only one result exisit?


for ($x=1;$x<=$totfound;$x++){
 	$result = $xml->xpath('//wp:listing['.$x.']/wp:address/wp:zip4');
	foreach ($result as $key => $value){
          $zip4[$x]=$value;
    }
}

Since you’re only wanting the first item in the $result array, then you can access it using $result[0]. Then, to push the number into the $zip4 array, you could use the following (the if is used because there might not be a zip4 for that listing).


if (!empty($result)) {
    $zip4[$x] = (string) $result[0];
}

From what I can understand of your code, it looks like you might be doing things in a pretty strange way which is forcing you to jump through hoops and do (possibly) more work than you have to. If you like, could you post more of your script, explaining what it’s trying to do?

$zip4[$x] = (string) $result[0];

This is what i wanted :smiley:
Thanks mate.