note 71728 deleted from ref.simplexml by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 May 2009 01:23:38 +0000 |
| Subject: | note 71728 deleted from ref.simplexml by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155526@lists.php.net to get a copy of this message | ||
Note Submitter: joshi at sencore dot com
----
One way to work around the issue that Arnaud presented on 19-Oct-2006 is to check for the existence
of the node before doing a 'foreach' on it.
<?php
$xml = simplexml_load_string('<root></root>');
if( $xml->b ) // Or maybe 'isset' or 'array_key_exists'
{
foreach($xml->b as $dummy);
}
echo $xml->asXML();
?>
Gives the expected result:
<?xml version="1.0"?>
<root/>
You can see the same behavior using an array, although you'll get a warning:
<?php
$p['a'] = '1';
foreach( $p['b'] as $node );
print_r( $p );
?>
Result:
Warning: Invalid argument supplied for foreach()
Array
(
[a] => 1
[b] =>
)