php - About SimpleXML's foreach -
php - About SimpleXML's foreach -
what's wrong here? want load list of 10 items, same. why?
<?php $xml = simplexml_load_file('test.xml'); $name = $xml->item->title; foreach($xml -> item $item){ echo "$name<br>"; } ?>
no, you're not accessing values within loop:
$name = $xml->item->title; // not this! foreach($xml->item $item){ // access `$item` within loop echo $item->title . '<br/>'; // echo "$name<br/>"; // you're accessing item outside loop }
additional question:
just trim title numbering:
$i = 1; $xml = simplexml_load_file('http://store.steampowered.com/feeds/weeklytopsellers.xml', null, libxml_nocdata); foreach($xml->item $item){ // $trimmed_title = ltrim($item->title, "#$i - "); $trimmed_title = str_replace("#$i - ", '', $item->title); echo $trimmed_title. '<br/>'; $i++; }
php xml simplexml
Comments
Post a Comment