You are here: Arrays not recognized in Simple XML Objects (using print_r) « PHP « IT news, forums, messages
Arrays not recognized in Simple XML Objects (using print_r)

Posted by Yashesh Bhatia on 10/12/77 11:11

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация