Arrays not recognized in Simple XML Objects (using print_r)

From: Date: Wed, 23 Mar 2005 08:46:17 +0000
Subject: Arrays not recognized in Simple XML Objects (using print_r)
Groups: php.general 
Request: Send a blank email to php-general+get-211366@lists.php.net to get a copy of this message
hi! i'm trying to use the simple_xml functions to get an arrray of data from a sample xml file. here's my sample_data.xml -------------------------------------------------------------------------------------------- <?xml version="1.0" encoding="ISO-8859-1"?> <sample_data> <first_names> <first_name>amit</first_name> <first_name>amar</first_name> </first_names> </sample_data> -------------------------------------------------------------------------------------------- the php file -------------------------------------------------------------------------------------------- <?php $sample_data = simplexml_load_file("sample_data.xml"); print_r($sample_data); print_r($sample_data->first_names); print_r($sample_data->first_names->first_name); ?> -------------------------------------------------------------------------------------------- output of "$ php test_xml_1.php" -------------------------------------------------------------------------------------------- SimpleXMLElement Object ( [first_names] => SimpleXMLElement Object
       (
           [first_name] => Array
               (
                   [0] => amit
                   [1] => amar
               )
       )
) SimpleXMLElement Object ( [first_name] => Array
       (
           [0] => amit
           [1] => amar
       )
) SimpleXMLElement Object ( [0] => amit ) -------------------------------------------------------------------------------------------- The above output shows $sample_data->first_names->first_name as an ARRAY in the first 2 print_r 's however when a print_r is run on itself it shows it as a SimpleXMLElementObject. Question is why the last print_r gives different information compared to the other 2. I also tried running $key = array_rand($sample_data->first_names->first_name) and it gives a warning message "Warning: array_rand(): First argument has to be an array in /home/yvb/work/practice/php/XML/foo.php on line 16" any clue how do i use the $sample_data->first_names->first_name as an array ? thx. yashesh. -- go pre http://www2.localaccess.com/rlalonde/pre.htm

« previous php.general (#211366) next »