2013-11-04 192 views
0

我從服務器得到了json響應(發佈如下)。爲了比較具有給定時間值的視頻的持續時間,目前我正在使用foreach循環分析每個object->durationresults數組。我有2個問題:優化php排序功能

  1. 是否有任何快速的方法來計算持續時間> 3600例如(而不是foreach方法)的對象?

  2. 目前使用以下if(內部foreach循環)項合併爲一個陣列:

     if ($duration >= $minDuration) { 
          $result [] = $tempResult; 
    

    其中($tempResult;results的當前對象)的任何快速的方式來接與來自陣列右持續時間的所有項目併合併成$result []?提前

    這裏

THX是JSON:

object(stdClass)[2] 
    public 'has_next' => boolean true 
    public 'next' => string 'http://rutube.ru/api/search/video/?order_by=rank&format=json&no_adult=checked&duration=long&query=%D0%B0%D0%B2%D0%B0%D1%82%D0%B0%D1%80&page=2' (length=141) 
    public 'previous' => null 
    public 'page' => int 1 
    public 'per_page' => int 10 
    public 'results' => 
    array (size=10) 
     0 => 
     object(stdClass)[3] 
      public 'id' => string '31518e25cde4d110f53d5e9ecde62395' (length=32) 
      public 'views' => int 1781 
      public 'all_tags' => 
      array (size=0) 
       ... 
      public 'author' => 
      object(stdClass)[4] 
       ... 
      public 'has_high_quality' => boolean false 
      public 'duration' => int 1477 
      public 'is_adult' => int 0 
      public 'thumbnail_url' => string 'http://pic.rutube.ru/video/e1/69/e169d576721998efe85e5a013091792b.jpg' (length=69) 
      public 'avatar' => string '' (length=0) 
      public 'html' => string '' (length=0) 
      public 'title' => string 'АВАТАР' (length=12) 
      public 'description' => string 'АВАТАР' (length=12) 
      public 'created_ts' => string '2010-08-08T14:50:32' (length=19) 
      public 'author_id' => int 0 
      public 'track_id' => int 3476481 
      public 'aname' => string '' (length=0) 
      public 'is_vip' => int 0 
      public 'tv_id' => int 0 
      public 'tv_title' => string '' (length=0) 
      public 'season' => int 0 
      public 'episode' => int 0 
      public 'category_id' => int 7 
      public 'is_licensed' => int 0 
      public 'duration_group' => int 5 
     1 => 
     object(stdClass)[5] 
      public 'id' => string 'e5b41d2d7080d5f30fe0e7fca41d5abc' (length=32) 
      public 'views' => int 2354 
      public 'all_tags' => 
      array (size=0) 
       ... 
      public 'author' => 
      object(stdClass)[6] 
       ... 
      public 'has_high_quality' => boolean false 
      public 'duration' => int 1442 
      public 'is_adult' => int 0 
      public 'thumbnail_url' => string 'http://pic.rutube.ru/video/ac/44/ac44fc73ab015c12fed5f419357c8216.jpg' (length=69) 
      public 'avatar' => string 'user/2b/11/2b1160ed4c17b20ab3e9534bb776e212.jpg' (length=47) 
      public 'html' => string '' (length=0) 
      public 'title' => string 'Аватар' (length=12) 
      public 'description' => string 'Аватар' (length=12) 
      public 'created_ts' => string '2011-01-15T00:07:50' (length=19) 
      public 'author_id' => int 17352 
      public 'track_id' => int 3984946 
      public 'aname' => string 'Uchiha_Itach1' (length=13) 
      public 'is_vip' => int 0 
      public 'tv_id' => int 0 
      public 'tv_title' => string '' (length=0) 
      public 'season' => int 0 
      public 'episode' => int 0 
      public 'category_id' => int 7 
      public 'is_licensed' => int 0 
      public 'duration_group' => int 5 
     2 => 
     object(stdClass)[7] 
      public 'id' => string 'f1c72b0afb5e5e9ca867a42af7889b26' (length=32) 
      public 'views' => int 336 
      public 'all_tags' => 
      array (size=0) 
       ... 
      public 'author' => 
      object(stdClass)[8] 
       ... 
      public 'has_high_quality' => boolean false 
      public 'duration' => int 1442 
      public 'is_adult' => int 0 
      public 'thumbnail_url' => string 'http://pic.rutube.ru/video/0e/6e/0e6ec1dbf789bf296cd4ed24b528545a.jpg' (length=69) 
      public 'avatar' => string 'user/2b/11/2b1160ed4c17b20ab3e9534bb776e212.jpg' (length=47) 
      public 'html' => string '' (length=0) 
      public 'title' => string 'Аватар' (length=12) 
      public 'description' => string 'Аватар' (length=12) 
      public 'created_ts' => string '2011-01-15T13:01:42' (length=19) 
      public 'author_id' => int 17352 
      public 'track_id' => int 3986119 
      public 'aname' => string 'Uchiha_Itach1' (length=13) 
      public 'is_vip' => int 0 
      public 'tv_id' => int 0 
      public 'tv_title' => string '' (length=0) 
      public 'season' => int 0 
      public 'episode' => int 0 
      public 'category_id' => int 7 
      public 'is_licensed' => int 0 
      public 'duration_group' => int 5 
     3 => 
     object(stdClass)[9] 
      public 'id' => string '41c9ac165dde0b35b8604c295772a41e' (length=32) 
      public 'views' => int 2684 
      public 'all_tags' => 
      array (size=0) 
       ... 
      public 'author' => 
      object(stdClass)[10] 
       ... 
      public 'has_high_quality' => boolean false 
      public 'duration' => int 1340 
      public 'is_adult' => int 0 
      public 'thumbnail_url' => string 'http://tub.rutube.ru/thumbs-wide/a0/e9/a0e92820a935e9ddd6c077532fba3749-1.jpg' (length=77) 
      public 'avatar' => string '' (length=0) 
      public 'html' => string '' (length=0) 
      public 'title' => string 'аватар ' (length=13) 
      public 'description' => string '' (length=0) 
      public 'created_ts' => string '2008-01-02T18:27:32' (length=19) 
      public 'author_id' => int 0 
      public 'track_id' => int 375879 
      public 'aname' => string '' (length=0) 
      public 'is_vip' => int 0 
      public 'tv_id' => int 0 
      public 'tv_title' => string '' (length=0) 
      public 'season' => int 0 
      public 'episode' => int 0 
      public 'category_id' => int 7 
      public 'is_licensed' => int 0 
      public 'duration_group' => int 5 
     4 => 
     object(stdClass)[11] 
      public 'id' => string 'ebcd7130e5bcb81be9087495325bcd80' (length=32) 
      public 'views' => int 331 
      public 'all_tags' => 
      array (size=0) 
       ... 
      public 'author' => 
      object(stdClass)[12] 
       ... 
      public 'has_high_quality' => boolean false 
      public 'duration' => int 1442 
      public 'is_adult' => int 0 
      public 'thumbnail_url' => string 'http://pic.rutube.ru/video/2b/7e/2b7ea182d936de40ea61c2f16cc9a9a6.jpg' (length=69) 
      public 'avatar' => string 'user/2b/11/2b1160ed4c17b20ab3e9534bb776e212.jpg' (length=47) 
      public 'html' => string '' (length=0) 
      public 'title' => string 'Аватар' (length=12) 
      public 'description' => string 's2e4-The Swamp.avi' (length=18) 
      public 'created_ts' => string '2011-01-16T13:13:46' (length=19) 
      public 'author_id' => int 17352 
      public 'track_id' => int 3989416 
      public 'aname' => string 'Uchiha_Itach1' (length=13) 
      public 'is_vip' => int 0 
      public 'tv_id' => int 0 
      public 'tv_title' => string '' (length=0) 
      public 'season' => int 0 
      public 'episode' => int 0 
      public 'category_id' => int 7 
      public 'is_licensed' => int 0 
      public 'duration_group' => int 5 
     5 => 
     object(stdClass)[13] 
      public 'id' => string '21e046d15b879c333677cd224f7f5c22' (length=32) 
      public 'views' => int 41019 
      public 'all_tags' => 
      array (size=1) 
       ... 
      public 'author' => 
      object(stdClass)[15] 
       ... 
      public 'has_high_quality' => boolean false 
      public 'duration' => int 2731 
      public 'is_adult' => int 0 
      public 'thumbnail_url' => string 'http://pic.rutube.ru/video/8f/94/8f94c671495b2b479dfa57b052dd2efb.jpg' (length=69) 
      public 'avatar' => string 'user/33/a1/33a1979db79e1878a9f4b0d1ce860423.jpg' (length=47) 
      public 'html' => string '' (length=0) 
      public 'title' => string 'Аватар: Легенда о Корре (2 книга) 1-2 серия [озв.Rain.Death]' (length=92) 
      public 'description' => string 'Аватар: Легенда о Корре [ТВ-2](2 сезон) 1-2 серия русская озвучка/ Avatar: The Legend of Korra (2 книга) 1-2 серия/ Аватар: Легенда о Корре [ТВ-2](2 сезон) 1-2 серия/ Avatar: The Legend of Korra (2 книга) 1-2 серия' (length=310) 
      public 'created_ts' => string '2013-09-14T21:34:27' (length=19) 
      public 'author_id' => int 12034 
      public 'track_id' => int 6552404 
      public 'aname' => string 'D_ENIS' (length=6) 
      public 'is_vip' => int 1 
      public 'tv_id' => int 0 
      public 'tv_title' => string '' (length=0) 
      public 'season' => int 0 
      public 'episode' => int 0 
      public 'category_id' => int 42 
      public 'is_licensed' => int 0 
      public 'duration_group' => int 5 
     6 => 
     object(stdClass)[16] 
      public 'id' => string '561d53734809ca73f97b41613cf4a390' (length=32) 
      public 'views' => int 25212 
      public 'all_tags' => 
      array (size=1) 
       ... 
      public 'author' => 
      object(stdClass)[18] 
       ... 
      public 'has_high_quality' => boolean false 
      public 'duration' => int 1423 
      public 'is_adult' => int 0 
      public 'thumbnail_url' => string 'http://pic.rutube.ru/video/a3/ad/a3ade856840ff611ccdc71fc296c7bf3.jpg' (length=69) 
      public 'avatar' => string 'user/33/a1/33a1979db79e1878a9f4b0d1ce860423.jpg' (length=47) 
      public 'html' => string '' (length=0) 
      public 'title' => string 'Аватар: Легенда о Корре (2 книга) 8 серия [озв.Rain.Death]' (length=90) 
      public 'description' => string 'Аватар: Легенда о Корре [ТВ-2](2 сезон) 8 (19) серия русская озвучка/ Avatar: The Legend of Korra (2 книга) 8 серия/ Аватар: Легенда о Корре ' (length=215) 
      public 'created_ts' => string '2013-10-19T16:21:11' (length=19) 
      public 'author_id' => int 12034 
      public 'track_id' => int 6612374 
      public 'aname' => string 'D_ENIS' (length=6) 
      public 'is_vip' => int 1 
      public 'tv_id' => int 0 
      public 'tv_title' => string '' (length=0) 
      public 'season' => int 0 
      public 'episode' => int 0 
      public 'category_id' => int 41 
      public 'is_licensed' => int 0 
      public 'duration_group' => int 5 
     7 => 
     object(stdClass)[19] 
      public 'id' => string '10d77328a0298537a61a616fba156814' (length=32) 
      public 'views' => int 6520 
      public 'all_tags' => 
      array (size=0) 
       ... 
      public 'author' => 
      object(stdClass)[20] 
       ... 
      public 'has_high_quality' => boolean false 
      public 'duration' => int 1464 
      public 'is_adult' => int 0 
      public 'thumbnail_url' => string 'http://pic.rutube.ru/video/31/a9/31a97a6067a6f3ac545099d913c26627.jpg' (length=69) 
      public 'avatar' => string 'user/33/a1/33a1979db79e1878a9f4b0d1ce860423.jpg' (length=47) 
      public 'html' => string '' (length=0) 
      public 'title' => string 'Аватар: Легенда о Корре 6 серия[озв.JonEA & Trina D]/ Th...' (length=86) 
      public 'description' => string 'Аватар: Легенда о Корре 6 серия,Аватар: Легенда о Корре 6 серия,Аватар: Легенда о Корре 6 серия,Аватар: Легенда о Корре 6 серия,Аватар: Легенда о Корре 6 серия,Аватар: Легенда о Корре 6 серия русская озвучка,The Last Airbender: The Legend of Korra 6 серия,The Last Airbender: The Legend of Korra 6 серия,The Last Airbender: The Legend of Korra '... (length=687) 
      public 'created_ts' => string '2012-05-23T13:53:24' (length=19) 
      public 'author_id' => int 12034 
      public 'track_id' => int 5644436 
      public 'aname' => string 'D_ENIS' (length=6) 
      public 'is_vip' => int 1 
      public 'tv_id' => int 0 
      public 'tv_title' => string '' (length=0) 
      public 'season' => int 0 
      public 'episode' => int 0 
      public 'category_id' => int 41 
      public 'is_licensed' => int 0 
      public 'duration_group' => int 5 
     8 => 
     object(stdClass)[21] 
      public 'id' => string '7805d6304c142b090bc04c58707a9a01' (length=32) 
      public 'views' => int 13909 
      public 'all_tags' => 
      array (size=0) 
       ... 
      public 'author' => 
      object(stdClass)[22] 
       ... 
      public 'has_high_quality' => boolean false 
      public 'duration' => int 1397 
      public 'is_adult' => int 0 
      public 'thumbnail_url' => string 'http://pic.rutube.ru/video/6e/8f/6e8f0937d5d36b6e276523a2df8c32a1.jpg' (length=69) 
      public 'avatar' => string 'user/33/a1/33a1979db79e1878a9f4b0d1ce860423.jpg' (length=47) 
      public 'html' => string '' (length=0) 
      public 'title' => string 'Аватар: Легенда о Корре 4 серия[озв.4upik]/ The Last Air...' (length=86) 
      public 'description' => string 'Аватар: Легенда о Корре 4 серия,Аватар: Легенда о Корре 4 серия,Аватар: Легенда о Корре 4 серия,Аватар: Легенда о Корре 4 серия,Аватар: Легенда о Корре 4 серия русская озвучка,The Last Airbender: The Legend of Korra 4 серия,The Last Airbender: The Legend of Korra 4 серия,The Last Airbender: The Legend of Korra 4 серия,The Last Airbender: The Legend of Korra 4 �'... (length=710) 
      public 'created_ts' => string '2012-04-28T13:17:41' (length=19) 
      public 'author_id' => int 12034 
      public 'track_id' => int 5574750 
      public 'aname' => string 'D_ENIS' (length=6) 
      public 'is_vip' => int 1 
      public 'tv_id' => int 0 
      public 'tv_title' => string '' (length=0) 
      public 'season' => int 0 
      public 'episode' => int 0 
      public 'category_id' => int 41 
      public 'is_licensed' => int 0 
      public 'duration_group' => int 5 
     9 => 
     object(stdClass)[23] 
      public 'id' => string '87b6019acda0e9f67091514d27a54eb2' (length=32) 
      public 'views' => int 14972 
      public 'all_tags' => 
      array (size=0) 
       ... 
      public 'author' => 
      object(stdClass)[24] 
       ... 
      public 'has_high_quality' => boolean false 
      public 'duration' => int 1451 
      public 'is_adult' => int 0 
      public 'thumbnail_url' => string 'http://pic.rutube.ru/video/33/7b/337b3120c6e6a9b43b8f57aefee0016e.jpg' (length=69) 
      public 'avatar' => string 'user/33/a1/33a1979db79e1878a9f4b0d1ce860423.jpg' (length=47) 
      public 'html' => string '' (length=0) 
      public 'title' => string 'Аватар: Легенда о Корре 9 серия[озв.Rain.Deat h]/ The La...' (length=86) 
      public 'description' => string 'Аватар: Легенда о Корре 9 серия,Аватар: Легенда о Корре 9 серия,Аватар: Легенда о Корре 9 серия,Аватар: Легенда о Корре 9 серия,Аватар: Легенда о Корре 9 серия,Аватар: Легенда о Корре 9 серия русская озвучка,The Last Airbender: The Legend of Korra 9 серия,The Last Airbender: The Legend of Korra 9 серия,The Last Airbender: The Legend of Korra '... (length=701) 
      public 'created_ts' => string '2012-06-10T21:49:28' (length=19) 
      public 'author_id' => int 12034 
      public 'track_id' => int 5692701 
      public 'aname' => string 'D_ENIS' (length=6) 
      public 'is_vip' => int 1 
      public 'tv_id' => int 0 
      public 'tv_title' => string '' (length=0) 
      public 'season' => int 0 
      public 'episode' => int 0 
      public 'category_id' => int 41 
      public 'is_licensed' => int 0 
      public 'duration_group' => int 5 
    public 'count' => int 1000 
+0

@ChrisLaplante我var_dumped從服務器的JSON響應值 – heron

+2

對不起,我誤解。但是,發佈JSON本身,而​​不是var_dump會更有幫助。 –

回答

0

你做正確。雖然您可以使用for循環而不是foreach並獲得幾微秒,但我不擔心這一點。你顯然是在下載一個json文件,所以如果你的文件非常大,'for'對'foreach'造成了很大的影響,你可能不會下載它。分配結果也是一樣。您正在製作每個匹配的副本並將其添加到新數組中 - 在大數據情況下,您可能會擔心內存使用情況,在這種情況下,您將使用for循環並跟蹤結果數組中的每個匹配的索引並從那裏出發。但是,再次,如果你正在處理下載一個json文件,我懷疑你會遇到上述任何一個問題,我會保持原樣。

0

爲PHP> = 5.3:

$results = array_filter($arr, function($var) { 
    return $var->duration > 3600; 
}); 

$count = count($results);