title: How to parse <media:content> tag in RSS with simplexml
body: <p>Structure of my RSS from <a href="http://rss.cnn.com/rss/edition.rss" rel="nofollow noreferrer">http://rss.cnn.com/rss/edition.rss</a> is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?&gt;
  &lt;?xml-stylesheet type="text/css" media="screen" href="http://rss.cnn.com/~d/styles/itemcontent.css"?&gt;
  &lt;rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/" version="2.0"&gt;
    &lt;channel&gt;
      &lt;title&gt;&lt;![CDATA[CNN.com - RSS Channel - Intl Homepage - News]]&gt;&lt;/title&gt;
      &lt;description&gt;&lt;![CDATA[CNN.com delivers up-to-the-minute news and information on the latest top stories, weather, entertainment, politics and more.]]&gt;&lt;/description&gt;
      &lt;link&gt;http://www.cnn.com/intl_index.html&lt;/link&gt;
      ...
  
      &lt;item&gt;
        &lt;title&gt;&lt;![CDATA[Russia responds to claims it has damaging material on Trump]]&gt;&lt;/title&gt;
        &lt;description&gt;&lt;![CDATA[The Kremlin denied it has compromising information about US President-elect Donald Trump, describing the allegations as "pulp fiction".]]&gt;&lt;/description&gt;
        &lt;link&gt;http://www.cnn.com/2017/01/11/politics/russia-rejects-trump-allegations/index.html&lt;/link&gt;
        &lt;guid isPermaLink="true"&gt;http://www.cnn.com/2017/01/11/politics/russia-rejects-trump-allegations/index.html&lt;/guid&gt;
        &lt;pubDate&gt;Wed, 11 Jan 2017 14:44:49 GMT&lt;/pubDate&gt;
        &lt;media:group&gt;
          &lt;media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-super-169.jpg" height="619" width="1100" /&gt;
          &lt;media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-large-11.jpg" height="300" width="300" /&gt;
          &lt;media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-vertical-large-gallery.jpg" height="552" width="414" /&gt;
          &lt;media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-video-synd-2.jpg" height="480" width="640" /&gt;
          &lt;media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-live-video.jpg" height="324" width="576" /&gt;
          &lt;media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-t1-main.jpg" height="250" width="250" /&gt;
          &lt;media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-vertical-gallery.jpg" height="360" width="270" /&gt;
          &lt;media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-story-body.jpg" height="169" width="300" /&gt;
          &lt;media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-t1-main.jpg" height="250" width="250" /&gt;
          &lt;media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-assign.jpg" height="186" width="248" /&gt;
          &lt;media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-hp-video.jpg" height="144" width="256" /&gt;
        &lt;/media:group&gt;
      &lt;/item&gt;
      ...
  
    &lt;/channel&gt;
  &lt;/rss&gt;
  </code></pre>
  
  <p>If you parse this XML with simplexml like this:</p>
  
  <pre><code>  $rss = simplexml_load_file($url, null, LIBXML_NOCDATA);
  
    $rssjson = json_encode($rss);
    $rssarray = json_decode($rssjson, TRUE);
  </code></pre>
  
  <p>you will see that <code>&lt;media:content&gt;</code> is simply missing in <code>$rssarray</code> items. So I found a <a href="http://www.marcogoncalves.com/2013/07/parsing-media-rss-with-php-simplexml/" rel="nofollow noreferrer">tutorial</a> with "namespace" solution. However, in the example author is using:</p>
  
  <pre><code>foreach ($xml-&gt;channel-&gt;item as $item) { ... }
  </code></pre>
  
  <p>but I am using (cannot use foreach for some reasons):</p>
  
  <pre><code>$rssjson = json_encode($rss);
  $rssarray = json_decode($rssjson, TRUE);
  </code></pre>
  
  <p>So I modified the solution for my case like this:</p>
  
  <pre><code>  $rss = simplexml_load_file($url, null, LIBXML_NOCDATA);
    $namespaces = $rss-&gt;getNamespaces(true); // get namespaces
  
    $rssjson = json_encode($rss);
    $rssarray = json_decode($rssjson, TRUE);
  
    if (isset($rssarray['channel']['item'])) {
      foreach ($rssarray['channel']['item'] as $key =&gt; $item) {
  
        $media_content = $rss-&gt;channel-&gt;item[$key]-&gt;children($namespaces['media']);
        foreach($media_content as $tag) {
  
          $tagjson = json_encode($tag);
          $tagarray = json_decode($tagjson, TRUE);
  
        }
  
      }
    }
  </code></pre>
  
  <p>But it does not work. For every item I get in <code>$tagarray</code> as a result an array with this structure:</p>
  
  <pre><code>Array(
    'content' =&gt; array(
       '0' =&gt; array(null),
       '1' =&gt; array(null),
       ...
       '11' =&gt; array(null),
     )
  )
  </code></pre>
  
  <p>It is an array with as many items as is the count of <code>&lt;media:content&gt;</code> tags, but every item is empty. I need to get an <code>url</code> attribute of every item. What am I doing wrong and getting an empty array?</p>