note 81342 added to ref.simplexml

From: Date: Sat, 23 Feb 2008 06:12:36 +0000
Subject: note 81342 added to ref.simplexml
Groups: php.notes 
Request: Send a blank email to php-notes+get-136435@lists.php.net to get a copy of this message
One more comment to Jason's XMLToArray function. The best and right way, which I found for multidimensional arrays and tags with the same names is this hook(see method convert). This will right save all data in right array without losing first element of same_nodes_name (this happens when we find first element, saves it and when find later one more with the same_nodes_name, we need to recreate array to save old data in [0] element of new array, otherwise our first element will be outside of array /see my previous post/). Also I made this little class for facility. final class XMLToArray { public function parse($xml) { return $this->convert(simplexml_load_string($xml)); } function convert($xml) { if ($xml instanceof SimpleXMLElement) { $children = $xml->children(); $return = null; } foreach ($children as $element => $value) { if ($value instanceof SimpleXMLElement) { $values = (array)$value->children(); if (count($values) > 0) { if (is_array($return[$element])) { //hook foreach ($return[$element] as $k=>$v) { if (!is_int($k)) { $return[$element][0][$k] = $v; unset($return[$element][$k]); } } $return[$element][] = $this->convert($value); } else { $return[$element] = $this->convert($value); } } else { if (!isset($return[$element])) { $return[$element] = (string)$value; } else { if (!is_array($return[$element])) { $return[$element] = array($return[$element], (string)$value); } else { $return[$element][] = (string)$value; } } } } } if (is_array($return)) { return $return; } else { return false; } } } ---- Server IP: 91.196.196.241 Probable Submitter: 194.187.49.15 ---- Manual Page -- http://www.php.net/manual/en/ref.simplexml.php Edit -- https://master.php.net/note/edit/81342 Del: integrated -- https://master.php.net/note/delete/81342/integrated Del: useless -- https://master.php.net/note/delete/81342/useless Del: bad code -- https://master.php.net/note/delete/81342/bad+code Del: spam -- https://master.php.net/note/delete/81342/spam Del: non-english -- https://master.php.net/note/delete/81342/non-english Del: in docs -- https://master.php.net/note/delete/81342/in+docs Del: other reasons-- https://master.php.net/note/delete/81342 Reject -- https://master.php.net/note/reject/81342 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#136435) next »