note 98748 added to book.simplexml

From: Date: Mon, 05 Jul 2010 08:22:41 +0000
Subject: note 98748 added to book.simplexml
Groups: php.notes 
Request: Send a blank email to php-notes+get-170700@lists.php.net to get a copy of this message
I have changed the simpleXMLToArray function from xananax at yelostudio dot com a little bit. Because the old function can not handle the xml file like this: test.xml <?xml version="1.0" encoding="UTF-8"?> <root> <fieldA /> <fieldA /> <fieldB /> <fieldB /> </root> Code i have changed: <?php /** * Converts a simpleXML element into an array. Preserves attributes and everything. * You can choose to get your elements either flattened, or stored in a custom index that * you define. * @param simpleXMLElement $xml the XML to convert * @param boolean $flattenValues Choose wether to flatten values * or to set them under a particular index. * defaults to true; * @param boolean $flattenAttributes Choose wether to flatten attributes * or to set them under a particular index. * Defaults to true; * @param boolean $flattenChildren Choose wether to flatten children * or to set them under a particular index. * Defaults to true; * @param string $valueKey index for values, in case $flattenValues was set to * false. Defaults to "@value" * @param string $attributesKey index for attributes, in case $flattenAttributes was set to * false. Defaults to "@attributes" * @param string $childrenKey index for children, in case $flattenChildren was set to * false. Defaults to "@children" * @return array the resulting array. */ function simpleXMLToArray($xml, $flattenValues=true, $flattenAttributes = true, $flattenChildren=true, $valueKey='@value', $attributesKey='@attributes', $childrenKey='@children'){ $return = array(); if(!($xml instanceof SimpleXMLElement)){return $return;} $name = $xml->getName(); $_value = trim((string)$xml); if(strlen($_value)==0){$_value = null;}; if($_value!==null){ if(!$flattenValues){$return[$valueKey] = $_value;} else{$return = $_value;} } $children = array(); $first = ""; foreach($xml->children() as $elementName => $child){ $value = simpleXMLToArray($child, $flattenValues, $flattenAttributes, $flattenChildren, $valueKey, $attributesKey, $childrenKey); if(isset($children[$elementName])){ if($first === "" || $first != $elementName){ $temp = $children[$elementName]; unset($children[$elementName]); $children[$elementName][] = $temp; $first=$elementName; } $children[$elementName][] = $value; } else{ $children[$elementName] = $value; } } if(count($children)>0){ if(!$flattenChildren){$return[$childrenKey] = $children;} else{$return = array_merge($return,$children);} } $attributes = array(); foreach($xml->attributes() as $name=>$value){ $attributes[$name] = trim($value); } if(count($attributes)>0){ if(!$flattenAttributes){$return[$attributesKey] = $attributes;} else{$return = array_merge($return, $attributes);} } return $return; } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 112.65.174.118 ---- Manual Page -- http://www.php.net/manual/en/book.simplexml.php Edit -- https://master.php.net/note/edit/98748 Del: integrated -- https://master.php.net/note/delete/98748/integrated Del: useless -- https://master.php.net/note/delete/98748/useless Del: bad code -- https://master.php.net/note/delete/98748/bad+code Del: spam -- https://master.php.net/note/delete/98748/spam Del: non-english -- https://master.php.net/note/delete/98748/non-english Del: in docs -- https://master.php.net/note/delete/98748/in+docs Del: other reasons-- https://master.php.net/note/delete/98748 Reject -- https://master.php.net/note/reject/98748 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#170700) next »