troubleshooting Question

php usort

Avatar of aline0508
aline0508 asked on
PHP
5 Comments1 Solution508 ViewsLast Modified:
I have this xml file from my YouTube account:
<?xml version="1.0"?>
<YouTubeVideos>
      <title>Video Title
            <video img="//i2.ytimg.com/vi/AhXBvK0x9fg/default.jpg" id="AhXBvK0x9fg" part="17"/>
            <video img="//i1.ytimg.com/vi/PzEdUMOBMRg/default.jpg" id="PzEdUMOBMRg" part="16"/>
            <video img="//i2.ytimg.com/vi/Q4-j5zQLr2A/default.jpg" id="Q4-j5zQLr2A" part="15"/>
                 ...
                 <video img="//i2.ytimg.com/vi/Q4-j5zQLr2A/default.jpg" id="Q4-j5zQLr2A" part="1"/>
       </title>
</YouTubeVideos>

Now I want to use usort() function to sort by part:

function sortByPart( $a, $b ){
    if ( $a[part] == $b[part] )
        return 0;
    if ( $a[part] < $b[part] )
         return -1;
    return 1;
}
            
$file = '../YouTubeVideos.xml';
$xml = simplexml_load_file($file);
if (!empty($xml->title)) {
   echo "<div id=\"div-wrap-around-a\">";
   foreach ($xml->title as $title) {
       $count = count($title);
       usort($title->video, 'sortByPart');
       foreach ( $title->video as $video ) {
             echo "<a href='#' id='{$video['id']}' title='$title Ph¿n {$video['part']}'><img src={$video['img']} width=50 align=\"absmiddle\" style=\"padding:5px\" /></a>" . " &nbsp; " .
                      "<a href='#' id='{$video['id']}' title='$title Ph¿n {$video['part']}'>".trim($title)." Ph¿n {$video['part']}</a>&nbsp;"."&nbsp;<img src=\"{$CCT_site['img_path']}videoicon.gif\" />" . "<br />";
          }
    }
    echo "</div>";
}

It's still displaying part 17 down to 1.

Thanks in advance
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros