note 93426 modified in function.simplexml-element-children by danbrown
| From: | danbrown@php.net | Date: | Wed, 09 Sep 2009 16:55:22 +0000 |
| Subject: | note 93426 modified in function.simplexml-element-children by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-160362@lists.php.net to get a copy of this message | ||
Simplied function based on one by rodmen at gmail dot com (with less nested output)
<?php
$Obj_SimpleXML = SimpleXML_Load_String($xml_doc);
function xml2phpArray($Obj_SimpleXML){
$siblings = array();
$children = $Obj_SimpleXML->children();
foreach($children as $child){
$key = $child->getName();
if($child->children()){
$siblings[$key] = xml2phpArray($child);
}
else {
$siblings[$key] = trim($child[0]);
}
}
return $siblings;
}
$output = xml2phpArray($Obj_SimpleXML)
print_r($output);
?>
--was--
Simplied function based on one by rodmen at gmail dot com (with less nested output)
$Obj_SimpleXML = SimpleXML_Load_String($xml_doc);
function xml2phpArray($Obj_SimpleXML){
$siblings = array();
$children = $Obj_SimpleXML->children();
foreach($children as $child){
$key = $child->getName();
if($child->children()){
$siblings[$key] = xml2phpArray($child);
}
else {
$siblings[$key] = trim($child[0]);
}
}
return $siblings;
}
$output = xml2phpArray($Obj_SimpleXML)
print_r($output);
http://php.net/manual/en/function.simplexml-element-children.php