Solved

php usort

Posted on 2011-09-06
5
403 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:aline0508
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 36487400
1
 
LVL 15

Accepted Solution

by:
babuno5 earned 500 total points
ID: 36487698
try this

if (!empty($xml->title)) {
   echo "<div id=\"div-wrap-around-a\">";
   foreach ($xml->title as $title) {
       $count = count($title);
         $newvideo = array();
         foreach($title->video as $tmp_new)
         {
                  $newvideo[] =array('id'=>(string)$tmp_new['id'],'part'=>(int)$tmp_new['part'],'img'=>(string)$tmp_new['img']);
         }
       usort($newvideo, 'sortByPart');
       foreach ( $newvideo 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>";
}
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36490180
I am having trouble making sense of the XML string.  Install this and run it to see why.
<?php // RAY_temp_aline0508.php
error_reporting(E_ALL);
echo "<pre>";


// TEST DATA FROM THE POST AT EE
$xml = <<<XML
<?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>
XML;


// MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml);

// VISUALIZE THE OBJECT
var_dump($obj);

Open in new window

0
 

Author Closing Comment

by:aline0508
ID: 36490248
Thanks.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36490299
If you make a small modification to the XML string, you can use something like this.
http://www.laprbass.com/RAY_temp_aline0508.php
Outputs:
<video img="//i2.ytimg.com/vi/Q4-j5zQLr2A/default.jpg" id="Q4-j5zQLr2A" part="1"/>
<video img="//i2.ytimg.com/vi/Q4-j5zQLr2A/default.jpg" id="Q4-j5zQLr2A" part="15"/>
<video img="//i1.ytimg.com/vi/PzEdUMOBMRg/default.jpg" id="PzEdUMOBMRg" part="16"/>
<video img="//i2.ytimg.com/vi/AhXBvK0x9fg/default.jpg" id="AhXBvK0x9fg" part="17"/>

No usort() complexity anywhere in sight!  HTH, ~Ray
<?php // RAY_temp_aline0508.php
error_reporting(E_ALL);
echo "<pre>";


// A DERIVATIVE OF THE TEST DATA FROM THE POST AT EE
$xml = <<<XML
<?xml version="1.0"?>
<YouTubeVideos>
   <YouTubeVideo>
      <title>Video Title</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"/>
   </YouTubeVideo>
</YouTubeVideos>
XML;


// MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml);

// ACTIVATE THIS TO VISUALIZE THE OBJECT
// var_dump($obj);

// COPY THE VIDEO IMG TAGS INTO A KEYED ARRAY
foreach ($obj->YouTubeVideo->video as $v)
{
    // CREATE A TAG AND AN ARRAY INDEX FROM THE OBJECT
    $tag = $v->AsXML();
    $ndx = (string)$v["part"];

    // USE THE PART NUMBER AS THE ARRAY INDEX KEY
    $arr[$ndx] = $tag;
}

// SORT BY THE KEYS
ksort($arr);

// SHOW THE WORK PRODUCT
foreach ($arr as $video_tag)
{
    echo PHP_EOL . htmlentities($video_tag);
}

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question