WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : XML To Array
Categories : PHP, PHP Classes, XML, Arrays Click here to Update Your Picture
MA Razzaque Rupom
Date : Jun 05th 2006
Grade : 5 of 5 (graded 4 times)
Viewed : 12841
File : 4416.zip
Images : No Images for this code example.
Search : More code by MA Razzaque Rupom
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

This class can be used to parse XML documents.

The parser results are stored in an associative array with the whole document structure.

Each array entry may contain nested arrays that describe the contents of the document tag structure.

The resultant array contains the exact hierarchy as of the given XML data.



class.xmltoarray.php
<?
/**
* XMLToArray Generator Class
* @author  :  MA Razzaque Rupom <rupom_315@yahoo.com>, <rupom.bd@gmail.com>
*             Moderator, phpResource (LINK1http://groups.yahoo.com/group/phpresource/LINK1)
*             URL: LINK2http://www.rupom.infoLINK2
* @version :  1.0
* @date       06/05/2006
* Purpose  : Creating Hierarchical Array from XML Data
* Released : Under GPL
*/

class XmlToArray
{
   
    var
$xml='';
   
   
/**
    * Default Constructor
    * @param $xml = xml data
    * @return none
    */
   
   
function XmlToArray($xml)
    {
       
$this->xml = $xml;   
    }
   
   
/**
    * _struct_to_array($values, &$i)
    *
    * This is adds the contents of the return xml into the array for easier processing.
    * Recursive, Static
    *
    * @access    private
    * @param    array  $values this is the xml data in an array
    * @param    int    $i  this is the current location in the array
    * @return    Array
    */
   
   
function _struct_to_array($values, &$i)
    {
       
$child = array();
        if (isset(
$values[$i]['value'])) array_push($child, $values[$i]['value']);
       
        while (
$i++ < count($values)) {
            switch (
$values[$i]['type']) {
                case
'cdata':
               
array_push($child, $values[$i]['value']);
                break;
               
                case
'complete':
                   
$name = $values[$i]['tag'];
                    if(!empty(
$name)){
                   
$child[$name]= ($values[$i]['value'])?($values[$i]['value']):'';
                    if(isset(
$values[$i]['attributes'])) {                   
                       
$child[$name] = $values[$i]['attributes'];
                    }
                }   
              break;
               
                case
'open':
                   
$name = $values[$i]['tag'];
                   
$size = isset($child[$name]) ? sizeof($child[$name]) : 0;
                   
$child[$name][$size] = $this->_struct_to_array($values, $i);
                break;
               
                case
'close':
                return
$child;
                break;
            }
        }
        return
$child;
    }
//_struct_to_array
   
    /**
    * createArray($data)
    *
    * This is adds the contents of the return xml into the array for easier processing.
    *
    * @access    public
    * @param    string    $data this is the string of the xml data
    * @return    Array
    */
   
function createArray()
    {
       
$xml    = $this->xml;
       
$values = array();
       
$index  = array();
       
$array  = array();
       
$parser = xml_parser_create();
       
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
       
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
       
xml_parse_into_struct($parser, $xml, $values, $index);
       
xml_parser_free($parser);
       
$i = 0;
       
$name = $values[$i]['tag'];
       
$array[$name] = isset($values[$i]['attributes']) ? $values[$i]['attributes'] : '';
       
$array[$name] = $this->_struct_to_array($values, $i);
        return
$array;
    }
//createArray
   
   
}//XmlToArray
?>



Usage Example
<?php
/**
* XMLToArray Generator Class
* @author  :  MA Razzaque Rupom <rupom_315@yahoo.com>, <rupom.bd@gmail.com>
*             Moderator, phpResource (http://groups.yahoo.com/group/phpresource/)
*             URL: http://www.rupom.info 
* @version :  1.0
* @date       06/05/2006
* Purpose  : Creating Hierarchical Array from XML Data
* Released : Under GPL
*/

require_once "class.xmltoarray.php";

//XML Data
$xml_data = "
<result>
   <studentname>
      MA Razzaque
   </studentname>
   <institute>
      RUET
   </institute>
   <dept>
      CSE
   </dept>
   <roll>
      99315
   </roll>
   <class>
      First
   </class>
</result>"
;

//Creating Instance of the Class
$xmlObj    = new XmlToArray($xml_data);
//Creating Array
$arrayData = $xmlObj->createArray();

//Displaying the Array
echo "<pre>";
print_r($arrayData);
echo
"</pre>";
?>



RSS parser. Parses RSS into an array. Quick and nasty but does the job. No checking is done for correct Tags, only correct XML. PHP4 needed to display result (uses print_r).
Categories : PHP, XML, PHP Classes, Rich Site Summary (RSS)
Array Insertion
Categories : PHP, PHP Classes, Arrays
A very basic and fast XML parser
Categories : PHP, PHP Classes, XML
HTML_Graphs uses PHP to provide a consistent interface for creating HTML based charts. The user of the class sets up arrays that are passed to html_graph() which then takes care of all the messy HTML layout.
Categories : Graphics, Arrays, PHP, PHP Classes, Charts and Graphs
PHPDRAW, the php wannabe Photoshop ;-)
Categories : PHP, PHP Classes, GD image library, Arrays
XML Menu
Categories : PHP, PHP Classes, Navigation, XML, XSL
Compare two texts and display a block of text with the differences between them.
Categories : PHP, PHP Classes, Filesystem, Strings, Arrays
Sort the results from a SELECT query (any number of columns) into an array automatically.
Categories : PHP, PHP Classes, Arrays, Databases, MySQL
Dynamic Loading of XML array data into ComboBox and Display XML data using PHP + DOM + Javascript.
Categories : PHP, Java Script, DOM XML, XML, Arrays
A class to put get and post variables in hidden form elements. Works on scalars, normal arrays, associative arrays.
Categories : Algorithms, Variables, Arrays, PHP, PHP Classes
Directory Listing To XML : Outputs XML File of a Given Directory Listing
Categories : PHP, PHP Classes, XML, Directories
Freshmeat.net XML-RPC - This class is meant to query Freshmeat for information about registered projects.
Categories : PHP, PHP Classes, XML, Web Services
Tweak Array, insert/add elements to any position of your arrays - delete elements from your arrays - move elements within your arrays - replace elements from your arrays ... the array, 'dynamically' grows or shrinks to whatever we tweak it.
Categories : PHP Classes, Arrays, PHP
DBXML- A Class to backup databases in XML Format using web interface
Categories : PHP, PHP Classes, Databases, MySQL, XML
TAB_STRUCT Class: Is supporting Class for the DBXML Class
Categories : PHP, PHP Classes, MySQL, XML, Databases