ここには基本的に 2 つのオプションがあります。使いやすいように、最初に項目を独自の変数に割り当てます。
$item = $items[$i];
そして、デバッグのための 2 つのオプション:
var_dump($item);
echo $item->asXML();
最初の行はvar_dump
、PHP である を作成します。この場合は、SimpleXML 固有のものです。
class SimpleXMLElement#193 (5) {
public $title =>
string(29) "Asylum seeker system overload"
public $link =>
string(29) "http://www.abc.net.au/bestof/"
public $description =>
class SimpleXMLElement#287 (0) {
}
public $pubDate =>
string(31) "Thu, 22 Nov 2012 00:00:00 +1100"
public $guid =>
string(8) "s3638457"
}
2 行目では、XML 自体が作成されます。
<item>
<title>Asylum seeker system overload</title>
<link>http://www.abc.net.au/bestof/</link>
<description><![CDATA[
<img style="float:right;" src="http://www.abc.net.au/common/images/news_asylum125.jpg" alt="Asylum seeker detainees (ABC News)">
<p>The Australian government is preparing to allow thousands of asylum seekers to love in the community.</p>
<ul>
<li><a href="http://mpegmedia.abc.net.au/news/lateline/video/201211/LATc_FedNauru_2111_512k.mp4">Watch (4:23)</a></li><li><a href="http://www.abc.net.au/lateline/content/2012/s3638174.htm">More - Lateline</a></li>
</ul>
]]></description>
<pubDate>Thu, 22 Nov 2012 00:00:00 +1100</pubDate>
<guid isPermaLink="false">s3638457</guid>
</item>
次の出力は表示されませんでした。
echo $items[$i];
その<item>
要素には値がなく、サブ要素しかないためです。例えば
echo $items[$i]->title;
文字列を出力します:
Asylum seeker system overload
これが役に立ち、光を当てることを願っています。ここでデモを見つけます。また、以下を利用できることも示していますforeach
。
$i = 0;
foreach ($rss->channel->item as $item)
{
if ($i++ == 2) {
var_dump($item);
echo $item->asXML(), "\n", $item->title;
}
}