开发者

Ordering Array by value

I'm pulling in a list of my vimeo albums using the Vimeo API and the looping three times through the array to get to the albums. It works fine.

My question is, I'm isolating the date, so how can I create a new array and sort it by the date?

While where at it, is there a way to jump to the third level of a multi-dimensional array?

    $albums=$vimeo->call('vimeo.albums.getAll', array('user_id' => $myUserId));

    $albums_array=object_2_array($albums);

    foreach($albums_array as $album_array_two){
        foreach($album_array_two as $album_array_three){
            foreach($album_array_threeas $album){
                if(stristr($album['title'],'conference')){
                    $title=$album['title'];
                    $description=$album['description'];
                    $date=stristr($album['description'],'.',true);
                    $year_comma=stristr($date,',');
                    $year=ereg_replace("[^0-9]", "", $year_comma);
                    $url_title='http://www.psfk.com/events/'.str_replace( " ", "-", strtolower($title));
                    $url='<a href="'.$url_title.'">'.$title.'</a>';
                    $thumb=$album['thumbnail_video']['thumbnails']['thumbnail'][1]['_content'];

                    echo '<li class="album">';
                    echo '<a href="'.$url_title.'"><img src="'.$thumb.'" alt="'.$title.'" /></a>';
                    echo '<div class="info">';
                    echo '<h2>'.$url.'</h2>';
                    echo $description.'<br />';
                    echo '<a href="'.$url_title.'">View...</a>';
                    echo '</div></li>';
                }
            }
        }
    }

Sample of the array returning one item:

    Array ( 
    [generated_in] => 0.0828 
    [stat] => ok 
    [albums] => Array ( 
        [on_this_page] => 7 
        [page] => 1 
        [perpage] => 50 
        [total] => 7 
        [album] => Array ( 
            [0] => Array ( 
                [id] => 1690236 
                [title] =>  Interviews 
                [description] => 
                [created_on] => 2011-09-10 21:43:49 
                [total_videos] => 1 
                [url] => Array ( 
                    [0] => http://vimeo.com/album/1690236 
                    ) 
                [video_sort_method] => 
                [thumbnail_video] => Array ( 
                    [id] => 28825158 
                    [owner] => 718882 
                    [title] =>  Where Inspir开发者_StackOverflow中文版ation Comes From [thumbnails] => Array ( 
                        [thumbnail] => Array ( 
                            [0] => Array ( 
                                [height] => 75 
                                [width] => 100 
                                [_content] => http://b.vimeocdn.com/ts/192/593/192593029_100.jpg 
                            ) 
                        )
                    )
                )
            )
        )
    )
)


In order to sort by date, you can use the php_function array_multisort(). There is a good example on that page that I think shows what you need. I'll try to provide a better example using your data. Suppose after looping through your albums you end up with an array $myAlbums that looks like this:

Array (
    [0] => Array(
        [title] => My Title
        [description] => some description
        [date] 01-05-2011
    )
    [1] => Array(
      .......
)

In order to sort this by date, you could do the following (taken from the example on the php page)

<?php
// Obtain a list of columns
foreach ($myAlbums as $key => $row) {
    $date[$key]  = $row['date'];
}

// Sort the data with volume descending, edition ascending
// Add $myAlbums as the last parameter, to sort by the common key
array_multisort($date, SORT_DESC, $myAlbums);
?>

Then you can print_r($myAlbums); and you should see that it is sorted. You might have to change the SORT_DESC flag depending on what formate your dates are in. I can't really explain HOW this works, because I'm still trying to figure it out myself... but I think it is what you need.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜