note 54950 deleted from ref.xml by bjori
| From: | bjori@php.net | Date: | Mon, 09 Oct 2006 11:13:20 +0000 |
| Subject: | note 54950 deleted from ref.xml by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-118288@lists.php.net to get a copy of this message | ||
Note Submitter: guyemup at yahoo dot com - heath
----
Expanding further on Vladson's/Alper's code, this will create an array of attributes
within the parent node called "{Parent} attributes". Probably isn't the most
eloquent of code, but, it works.
function xml2array( $textXml )
{
$regExElements = '/<(\w+)([^>]*)>(.*?)<\/\\1>/s';
$regExAttributes = '/(\w+)="([^"]*)"/';
preg_match_all( $regExElements, $textXml, $matchElements );
foreach ( $matchElements[1] as $keyElements=>$valElements ) {
if ( $matchElements[2][$keyElements] )
{
preg_match_all( $regExAttributes, $matchElements[2][$keyElements], $matchAttributes );
foreach ( $matchAttributes[0] as $keyAttributes=>$valAttributes )
{
$arrayAttributes[ $valElements.' attributes' ][$matchAttributes[1][ $keyAttributes ] ]
= $matchAttributes[2][ $keyAttributes ];
}
}
else
{
$arrayAttributes = null;
}
if ( preg_match( $regExElements, $matchElements[3][$keyElements]) ) {
if ( $arrayAttributes )
{
$arrayFinal[ $valElements ][ $valElements.' attributes' ] = $arrayAttributes[
$valElements.' attributes' ];
}
$arrayFinal[ $valElements ][] = xml2array( $matchElements[3][$keyElements] );
}
else
{
$arrayFinal[ $valElements ] = $matchElements[3][ $keyElements ];
$arrayFinal = array_merge( $arrayFinal, $arrayAttributes );
}
}
return $arrayFinal;
}