note 54625 deleted from ref.xml by bjori
| From: | bjori@php.net | Date: | Mon, 09 Oct 2006 11:06:10 +0000 |
| Subject: | note 54625 deleted from ref.xml by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-118263@lists.php.net to get a copy of this message | ||
Note Submitter: alper at sabanciuniv dot edu
----
vladson has written a good code to converting an xml to an array. It is a good function but it is
not work properly if xml elements has an attribute. I have cahanged the code to work properly when
it has attributes.
The code is:
function xml2array($text) {
$reg_exp = '/<(\w+)[^>]*>(.*?)<\/\\1>/s';
preg_match_all($reg_exp, $text, $match);
foreach ($match[1] as $key=>$val) {
if ( preg_match($reg_exp, $match[2][$key]) ) {
$array[$val][] = xml2array($match[2][$key]);
} else {
$array[$val] = $match[2][$key];
}
}
return $array;
}
Alper Sari