note 54950 added to ref.xml

From: Date: Wed, 20 Jul 2005 15:24:57 +0000
Subject: note 54950 added to ref.xml
Groups: php.notes 
Request: Send a blank email to php-notes+get-92216@lists.php.net to get a copy of this message
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; } ---- Manual Page -- http://www.php.net/manual/en/ref.xml.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+54950 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54950&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54950&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54950&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54950&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54950&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54950&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+54950&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#92216) next »