Hi,
I've had a look round similar articles such as this one and I can't quite get it to work, quite possible I'm just misunderstanding.
I've got a simple script which parses a bit of xml and prints out specific fields - what I'm having trouble doing is accessing the data of SimpleXMLElement Objects.
XML (simplified for clarity)
<channel>
<item>
<title><![CDATA[Title is in here ...]]></title>
<description>Our description is in here!</description>
</item>
</channel>
PHP
$url = "file.xml";
$xml = simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA);
foreach ($xml->channel->item as $item) {
$articles = array();
$articles['title'] = $item->title;
$articles['description'] = $item->description;
}
Up to this point, everything seems ok. I end up with an array of content which I can confirm with print_r, this is what I get back:
Array
(
[title] => SimpleXMLElement Object
(
[0] => Title is in here ...
)
[description] => SimpleXMLElement Object
(
[0] => Our description is in here!
)
)
The key question
How do I then access [title][0] or [description][0]?
I've tried a couple of variations with no success, most likely a rookie error somewhere!
foreach ($articles as $article) {
echo $article->title;
}
and
foreach ($articles as $article) {
echo $article['title'][0];
}
and
foreach ($articles as $article) {
echo $article['title'];
}