note 41808 modified in ref.simplexml by aidan
| From: | aidan@php.net | Date: | Mon, 28 Jun 2004 00:35:29 +0000 |
| Subject: | note 41808 modified in ref.simplexml by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-71972@lists.php.net to get a copy of this message | ||
In reply to <ash at bigfoot dot de>
The simpleXMLtoArray function you propose is actually the same as doing:
<?php
include 'example.php';
$xml = simplexml_load_string($xmlstr);
$ar = (array) $xml;
?>
It has besides that one little problem. It drops the attribute information.
The following function also takes in account the attributes. It treats them as if they where
children.
<?php
function &simplexml_to_array($xml)
{
$ar = array();
foreach( $xml->children() as $k => $v ) {
// recurse the child
$child = simplexml_to_array( $v );
// if it's not an array, then it was empty, thus a value/string
if( count($child) == 0 ) {
$child = (string)$v;
}
// add the childs attributes as if they where children
foreach( $v->attributes() as $ak => $av ) {
// if the child is not an array, transform it into one
if( !is_array( $child ) ) {
$child = array( "value" => $child );
}
$child[$ak] = (string)$av;
}
// if the $k is already in our children list, we need to transform
// it into an array, else we add it as a value
if( !in_array( $k, array_keys($ar) ) ) {
$ar[$k] = $child;
} else {
$ar[$k] = array( $ar[$k] );
$ar[$k][] = $child;
}
}
return $ar;
}
$xml = simplexml_load_string($xmlstr);
print_r( simplexml_to_array( $xml ) );
?>
The resulting array looks like this :
Array
(
[movie] => Array
(
[title] => PHP: Behind the Parser
[characters] => Array
(
...
)
I hope this can help someone. I needed this because you cannot put a simpleXML object into eg. a
session due to serialization issues. This way I can store the information inside the XML document,
eg. in a session, eliminating the need to reparse it time after time while still having a rather
nice interface for accessing the data.
--was--
In reply to : ash at bigfoot dot de
The simpleXMLtoArray function you propose is actually the same as doing:
<?php
include 'example.php';
$xml = simplexml_load_string($xmlstr);
$ar = (array) $xml;
?>
It has besides that one little problem. It drops the attribute information.
The following function also takes in account the attributes. It treats them as if they where
children.
<?php
function &simplexml_to_array( $xml ) {
$ar = array();
foreach( $xml->children() as $k => $v ) {
// recurse the child
$child = simplexml_to_array( $v );
// if it's not an array, then it was empty, thus a value/string
if( count($child) == 0 ) {
$child = (string)$v;
}
// add the childs attributes as if they where children
foreach( $v->attributes() as $ak => $av ) {
// if the child is not an array, transform it into one
if( !is_array( $child ) ) {
$child = array( "value" => $child );
}
$child[$ak] = (string)$av;
}
// if the $k is already in our children list, we need to transform
// it into an array, else we add it as a value
if( !in_array( $k, array_keys($ar) ) ) {
$ar[$k] = $child;
} else {
$ar[$k] = array( $ar[$k] );
$ar[$k][] = $child;
}
}
return $ar;
}
include 'example.php';
$xml = simplexml_load_string($xmlstr);
print_r( simplexml_to_array( $xml ) );
?>
The resulting array looks like this :
Array
(
[movie] => Array
(
[title] => PHP: Behind the Parser
[characters] => Array
(
[character] => Array
(
[0] => Array
(
[name] => Ms. Coder
[actor] => Onlivia Actora
)
[1] => Array
(
[name] => Mr. Coder
[actor] => El ActÃr
)
)
)
[plot] =>
So, this language. It's like, a programming language. Or is it a
scripting language? All is revealed in this thrilling horror spoof
of a documentary.
[rating] => Array
(
[0] => Array
(
[value] => 7
[type] => thumbs
)
[1] => Array
(
[value] => 5
[type] => stars
)
)
)
)
I hope this can help someone. I needed this because you cannot put a simpleXML object into eg. a
session due to serialization issues. This way I can store the information inside the XML document,
eg. in a session, eliminating the need to reparse it time after time while still having a rather
nice interface for accessing the data.
http://php.net/manual/en/ref.simplexml.php