note 54914 rejected from function.simplexml-load-file by danbrown
| From: | danbrown@php.net | Date: | Thu, 24 Jul 2008 13:32:39 +0000 |
| Subject: | note 54914 rejected from function.simplexml-load-file by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142657@lists.php.net to get a copy of this message | ||
Note Submitter: joelfielder at hotmail dot com
----
In the object2array function posted above, the following data structure would be left unchanged:
Array
(
[0] => Object Object
(
[var] => 1
)
)
The simplexml_load_... functions return structures similar to the above, a simple example:
SimpleXMLElement Object
(
[USERS] => Array
(
[0] => SimpleXMLElement Object
(
[NAME] => Joel Fielder
[EMAIL] => joelfielder@hotmail.com
)
)
)
In order to store such information in the session, we have to convert all of the SimpleXMLElement
objects present in the structure:
Array
(
[USERS] => Array
(
[0] => Array
(
[NAME] => Joel Fielder
[EMAIL] => joelfielder@hotmail.com
)
)
)
And here is the code to do so:
<?php
function object2array($object)
{
$return = NULL;
if(is_array($object))
{
foreach($object as $key => $value)
$return[$key] = object2array($value);
}
else
{
$var = get_object_vars($object);
if($var)
{
foreach($var as $key => $value)
$return[$key] = object2array($value);
}
else
return $object;
}
return $return;
}
?>