2013-10-02 109 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