php - How to display XML nodes that have the same name? -


i have xml blog feed displaying on wordpress. here simple representation of xml nodes:

<item>     <title>       title     </title>     <description>       description     </description>     <category>       category 1     </category>     <category>       category 2     </category>     <category>       category 3     </category>  </item> 

so you'll notice above category node displayed 3 times has same name. when i'm using below php display xml nodes in loop can 1 of category nodes rest aren't unique.

does know how can display of category nodes please???

<?php     $rss = new domdocument();     $rss->load('http://blog.com/rss.xml');     $feed = array();     foreach ($rss->getelementsbytagname('item') $node) {       $item = array (         'title' => $node->getelementsbytagname('title')->item(0)->nodevalue,         'desc' => $node->getelementsbytagname('description')->item(0)->nodevalue,         'category' => $node->getelementsbytagname('category')->item(0)->nodevalue,         );       array_push($feed, $item);     }   ?>  <?php   $limit = 3;   for($x=0;$x<$limit;$x++) {   $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);   $description = $feed[$x]['desc'];   $category = $feed[$x]['category'];    echo $title;   echo $desc;   echo $category;   } ?> 

saving categories instead of 1 category each item:

<?php  function domnodelist2array($node_list) {     $nodes = [];     foreach ($node_list $node) {         $nodes[] = $node;     }     return $nodes; }  $rss = new domdocument(); $rss->load('rss.xml'); $feed = array(); foreach ($rss->getelementsbytagname('item') $node) {     $item = [         'title' => $node->getelementsbytagname('title')->item(0)->nodevalue,         'desc' => $node->getelementsbytagname('description')->item(0)->nodevalue,         'categories' => array_map(function ($item) {             return $item->nodevalue;         }, domnodelist2array($node->getelementsbytagname('category')))];     array_push($feed, $item); }   $limit = 1; ($x = 0; $x < $limit; $x++) {     $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);     $description = $feed[$x]['desc'];     $categories = $feed[$x]['categories'];      echo $title;     echo $description;     foreach ($categories $category) {         echo $category;     } } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -