Accessing SimpleXMLElements within an array
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) 开发者_JS百科{
$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'];
}
If you really don't want to simply pass the SimpleXMLelement but put the values in an array first....
<?php
// $xml = simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA);
$xlm = getData();
$articles = array();
foreach ($xml->channel->item as $item) {
// with (string)$item->title you get rid of the SimpleXMLElements and store plain strings
// but you could also keep the SimpleXMLElements here - the output is still the same.
$articles[] = array(
'title'=>(string)$item->title,
'description'=>(string)$item->description
);
}
// ....
foreach( $articles as $a ) {
echo $a['title'], ' - ', $a['description'], "\n";
}
function getData() {
return new SimpleXMLElement('<foo><channel>
<item>
<title><![CDATA[Title1 is in here ...]]></title>
<description>Our description1 is in here!</description>
</item>
<item>
<title><![CDATA[Title2 is in here ...]]></title>
<description>Our description2 is in here!</description>
</item>
</channel></foo>');
}
prints
Title1 is in here ... - Our description1 is in here!
Title2 is in here ... - Our description2 is in here!
I think you have an error when you assign value to to array:
foreach ($xml->channel->item as $item) {
$articles = array();
$articles['title'] = $item->title;
$articles['description'] = $item->description;
}
if you have foreach why are your creating on every step new array $articles = array();
$articles = array();
foreach ($xml->channel->item as $item) {
$articles['title'] = $item->title;
$articles['description'] = $item->description;
}
精彩评论