0

I have the following code:

$rss = simplexml_load_file( 'http://gdata.youtube.com/feeds/api/playlists/PLEA1736AA2720470C?v=2&prettyprint=true' );

foreach ( $rss->entry as $entry ) {
    // get nodes in media: namespace for media information
    $media = $entry->children( 'http://search.yahoo.com/mrss/' );
    $thumbs = $media->group->thumbnail;
    $thumb_attrs = array();
    $index = 0;
    // get thumbnails attributes: url | height | width
    foreach ( $thumbs as $thumb ) {
        foreach ( $thumb->attributes() as $attr => $value ) {
            $thumb_attrs[$index][$attr] = $value;
            print $attr . ': ' . $thumb_attrs[$index][$attr] . "| ";
        }
        $index++;
        print  "<br>";
    }
}

The print will output:

url: http://i.ytimg.com/vi/te28_L-dO88/default.jpg| height: 90| width: 120| time: 00:00:49| 
...

from the xml tags with the following format:

<media:thumbnail url='http://i.ytimg.com/vi/4l4rwvAPhfA/default.jpg' height='90' width='120' time='00:02:23.500' yt:name='default'/>
...

How can I add the attribute with namespace yt name = 'default' that I'm not getting to the array?

How can I get the closest value of all width to other value from the array? Something similar to PHP - Nearest value from an array but taking into account that my array is multidimensional.

Community
  • 1
  • 1
PoseLab
  • 1,841
  • 1
  • 16
  • 22

1 Answers1

0

The problem with simplexml and namespaces is that you have to access any namespaced elements or attributes by name -- that is, you can't say, "give me all attributes regardless of namespace." So you have to do some looping, relying on simplexml's namespacing tools:

$rss = simplexml_load_file( 'http://gdata.youtube.com/feeds/api/playlists/PLEA1736AA2720470C?v=2&prettyprint=true' );
$namespaces=$rss->getNameSpaces(true); // access all the namespaces used in the tree
array_unshift($namespaces,""); // add a blank at the beginning of the array to deal with the unprefixed default
foreach ( $rss->entry as $entry ) {
    // get nodes in media: namespace for media information
    $media = $entry->children( 'http://search.yahoo.com/mrss/' );
    $thumbs = $media->group->thumbnail;
    $thumb_attrs = array();
    $index = 0;
    // get thumbnails attributes: url | height | width
    foreach ( $thumbs as $thumb ) {
        $attrstring="";
        foreach ($namespaces as $ns) {
                foreach ( $thumb->attributes($ns) as $attr => $value ) { // get all attributes, whatever namespace they might be in
                        $thumb_attrs[$index][$attr] = $value;
                        $attrstring.=$attr . ': ' . $thumb_attrs[$index][$attr] . "| ";
                }
        }
        print $attrstring;
        $index++;
        print  "<br>";
    }
}

As for the second part of your question, I'm not 100% sure what you're asking. If it really is similar to the question you link to, wouldn't you just be able to create an empty array before your loop, and add the width of each entry to that array? When your loop is done, you'll have a flattened array of just the widths.

But if you're asking something else, perhaps you can clarify?

jlmcdonald
  • 13,408
  • 2
  • 54
  • 64