按值排序数组

时间:2011-09-11 02:22:09

标签: php arrays vimeo

我正在使用Vimeo API提取我的vimeo专辑列表,并在数组中循环三次以进入相册。它工作正常。

我的问题是,我正在隔离日期,那么如何创建一个新数组并按日期对其进行排序?

虽然在哪里,有没有办法跳转到多维数组的第三级?

    $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>';
                }
            }
        }
    }

返回一个项目的数组示例:

    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 Inspiration Comes From [thumbnails] => Array ( 
                        [thumbnail] => Array ( 
                            [0] => Array ( 
                                [height] => 75 
                                [width] => 100 
                                [_content] => http://b.vimeocdn.com/ts/192/593/192593029_100.jpg 
                            ) 
                        )
                    )
                )
            )
        )
    )
)

1 个答案:

答案 0 :(得分:3)

为了按日期排序,您可以使用php_function array_multisort()。我认为该页面上有一个很好的例子可以显示您需要的内容。我将尝试使用您的数据提供更好的示例。假设在遍历您的相册后,您最终会得到一个如下所示的数组$myAlbums

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

为了按日期对此进行排序,您可以执行以下操作(取自php页面上的示例)

<?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);
?>

然后你可以print_r($myAlbums);,你应该看到它已经排序了。您可能需要更改SORT_DESC标志,具体取决于您的日期格式。我无法解释这是如何工作的,因为我仍在努力弄明白自己...但我认为它是你需要什么。