2011-09-11 44 views
0

我正在使用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 
          ) 
         ) 
        ) 
       ) 
      ) 
     ) 
    ) 
) 
+0

你可以發表什麼樣的數組樣本?我很困惑,爲什麼有三個級別的專輯陣列 – bcoughlan

+0

數組添加到問題。 – ok1ha

回答

2

按日期排序,你可以使用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標誌,具體取決於您的日期所在的日期。我無法真正解釋這是如何工作的,因爲我仍在試圖自己弄清楚......但我認爲這是您需要的。