Question : xml to array

i have a xml file like below
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
<?xml version="1.0" encoding="UTF-8"?>
<courses>
	<course>
		<name>Course 1</name>
		<exam>
			<title>Exam 1</title>
			<date>2010</date>
			<grade>60</grade>		
		</exam>
		<exam>
			<title>Exam 2</title>
			<date>2010</date>
			<grade>70</grade>		
		</exam>	
	</course>
	<course>
		<name>Course 2</name>
		<exam>
			<title>Exam 3</title>
			<date>2010</date>
			<grade>50</grade>
		</exam>
	</course>
</courses>


and i am converting this xml to array code like below
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
function objectsIntoArray($arrObjData, $arrData = array())
{

	// if input is object, convert into array
	if (is_object($arrObjData)) {
		$arrObjData = get_object_vars($arrObjData);
	}

	if (is_array($arrObjData)) {
			foreach ($arrObjData as $index => $value) {
				if (is_object($value) or is_array($value)) {
					$value = objectsIntoArray($value); //recursive call
				}
				
				$arrData[$index] = $value;
			}
	}

	return $arrData;
}

$xml = "my.xml";

$object = simplexml_load_file($xml);

$arr = objectsIntoArray($object);

print "<pre>";

print_r($arr);

echo "Course Count ".count($arr[course])."<br/>";

foreach ($arr[course] as $course_key => $course_value){
	echo "Exam Count ".count($arr[course][$course_key][exam])."<br/>";
}


now please look Course 2 there is an exam but it prints me 3. because when there is one exam it not converts to array
exam [0] array{

}

it converts directly exam array{

}

because of that it count not exams it count tags inside exam.

how can i fix this.

thanks.

Answer : xml to array

if you use this code below you will understand what i mean. this code solved my problem.
[code]
<?
/**
* XMLToArray Generator Class
* @author  :  MA Razzaque Rupom <[email protected]>, <[email protected]>
*             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
?>
<html>
      <head>
            <meta http-equiv="content-type" content="text/html; charset=utf-8">
      </head>
      <body>
            <?php
            $xml_data = file_get_contents("XMLFormatPerformanceReport.xml");
            
            //Creating Instance of the Class
            $xmlObj    = new XmlToArray($xml_data);
            
            //Creating Array
            $arrayData = $xmlObj->createArray();
            
            //Displaying the Array
            echo "<pre>";
            print_r($arrayData);
            ?>      
      </body>
</html>
[/code]
Random Solutions  
 
programming4us programming4us