note 71728 added to ref.simplexml
| From: | joshi at sencore dot com | Date: | Wed, 13 Dec 2006 15:10:54 +0000 |
| Subject: | note 71728 added to ref.simplexml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-120579@lists.php.net to get a copy of this message | ||
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] =>
)
----
Server IP: 64.71.164.2
Probable Submitter: 216.16.37.11
----
Manual Page -- http://www.php.net/manual/en/ref.simplexml.php
Edit -- https://master.php.net/note/edit/71728
Del: integrated -- https://master.php.net/note/delete/71728/integrated
Del: useless -- https://master.php.net/note/delete/71728/useless
Del: bad code -- https://master.php.net/note/delete/71728/bad+code
Del: spam -- https://master.php.net/note/delete/71728/spam
Del: non-english -- https://master.php.net/note/delete/71728/non-english
Del: in docs -- https://master.php.net/note/delete/71728/in+docs
Del: other reasons-- https://master.php.net/note/delete/71728
Reject -- https://master.php.net/note/reject/71728
Search -- https://master.php.net/manage/user-notes.php