2013-10-02 159 views
-1

我有這樣的陣列:排序PHP多維數組[值1] [值]

[0] => Array 
    (


     [tags] => SimpleXMLElement Object 
      (
       [0] => MusicVideos, #20 
      ) 

    ) 

[1] => Array 
    (

     [tags] => SimpleXMLElement Object 
      (
       [0] => MusicVideos, #5 
      ) 

    ) 

[2] => Array 
    (


     [tags] => SimpleXMLElement Object 
      (
       [0] => #9, MusicVideos 
      ) 

    ) 

[3] => Array 
    (

     [tags] => SimpleXMLElement Object 
      (
       [0] => #12, MusicVideos 
      ) 

    ) 

現在我想排序這個數組通過[標籤]我如何做到這一點這個PHP? 使用在線工具[標籤]用在我的網站上訂購的,我想「#1」,「2號」

但我有比[標籤]象「#1」和「類別」

一個更

[Tags] => #1 [Tags] => MusicVideos, #2 [Tags] => #3

是否有可能這樣?

我嘗試使用此功能:

function msort($array, $key, $sort_flags = SORT_NUMERIC) { 
if (is_array($array) && count($array) > 0) { 
    if (!empty($key)) { 
     $mapping = array(); 
     foreach ($array as $k => $v) { 
      $sort_key = ''; 
      if (!is_array($key)) { 
       $sort_key = $v[$key]; 
      } else { 
       // @TODO This should be fixed, now it will be sorted as string 
       foreach ($key as $key_key) { 
        $sort_key .= $v[$key_key]; 
       } 
       $sort_flags = SORT_STRING; 
      } 
      $mapping[$k] = $sort_key; 
     } 
     asort($mapping, $sort_flags); 
     $sorted = array(); 
     foreach ($mapping as $k => $v) { 
      $sorted[] = $array[$k]; 
     } 
     return $sorted; 
    } 
} 
return $array; 

}

但是,這種由所有名稱和下一個號碼

[0] => Array 
    (
     [tags] => SimpleXMLElement Object 
      (
       [0] => #5 
      ) 

    ) 

[1] => Array 
    (
     [tags] => SimpleXMLElement Object 
      (
       [0] => Commercials, #3 
      ) 

    ) 

[2] => Array 
    (
     [tags] => SimpleXMLElement Object 
      (
       [0] => Commercials, #9 
      ) 


    ) 

[3] => Array 
    (
     [tags] => SimpleXMLElement Object 
      (
       [0] => MusicVideos, #1 
      ) 

    ) 

[4] => Array 
    (
     [tags] => SimpleXMLElement Object 
      (
       [0] => MusicVideos, #10 
      ) 


    ) 

後引進空的,我想是這樣的:

[0] => Array 
    (
     [tags] => SimpleXMLElement Object 
      (
       [0] => MusicVideos, #1 
      ) 

    ) 

[1] => Array 
    (
     [tags] => SimpleXMLElement Object 
      (
       [0] => Commercials, #3 
      ) 

    ) 

[2] => Array 
    (
     [tags] => SimpleXMLElement Object 
      (
       [0] => #5 
      ) 

    ) 

[3] => Array 
    (
     [tags] => SimpleXMLElement Object 
      (
       [0] => Commercials, #9 
      ) 

    ) 

[4] => Array 
    (
     [tags] => SimpleXMLElement Object 
      (
       [0] => MusicVideos, #10 
      ) 

    ) 

謝謝

+0

是標籤本身(在「標籤'字段)已經按排序順序?即我會找到包含「MusicVideos,#5」的'tags'字段嗎?如果是這樣''MusicVideos,#5'在#12之前排序,MusicVideos? –

+0

我使用VIMEO API,有時他們有時不排序或更改標籤的順序。 如果可能,我想要搜索數組在哪裏(「#1」,「#2」)並按順序排列,有可能嗎? – diogoroseiro

+0

你可以使用[array_multisort()](http://php.net/manual/en/function.array-multisort.php#example-4927),我不明白你想排序 –

回答

0

你應該能夠使用usort與適當的callable做排序。

排序功能可以使用任何邏輯來提取排序字段。如果你想通過與#後跟一個數字開始的標籤排序你需要找到在標籤領域的子字符串(這裏使用正則表達式):

usort($arr, function($a, $b) { 
    $matches = array(); 
    preg_match('/#(\d+)/', (string) $a['tags'], $matches); 
    $aTagNo = (int) $matches[1]; 
    preg_match('/#(\d+)/', (string) $b['tags'], $matches); 
    $bTagNo = (int) $matches[1]; 

    if ($aTagNo < $bTagNo) 
     return -1; 
    else if ($aTagNo > $bTagNo) 
     return 1; 
    else 
     return 0; 
}); 
+0

HI!並感謝幫助,但我嘗試使用,但顯示'分析錯誤:語法錯誤,意外的T_STRING' 我已更新帖子也許幫助你更多瞭解:s – diogoroseiro

+0

哎呀,很多缺少分號,在Go編碼將做到這一點給你。修正了,雖然我想知道如果你自己無法修復它,你有多熟悉PHP。 –

+0

我有更新的問題,也許這種方式,你可以幫助我更好:) 非常感謝您嘗試 – diogoroseiro