2015-12-04 63 views
1

排序multidimesional陣列我有一個這樣的數組:PHP的第一個關鍵

array 
    'list_10' => 
    array 
     row_0 => 
     array 
      'Id' => string '118579' 
      'Status' => string '3' 
     row_1 => 
     array 
      'Id' => string '117662' 
      'Status' => string '2' 
     row_2 => 
     array 
      'Id' => string '117662' 
      'Status' => string '2' 
    'list_11' => 
    array 
     row_0 => 
     array 
      'Id' => string '112564' 
      'Status' => string '2' 
     row_1 => 
     array 
      'Id' => string '153622' 
      'Status' => string '3' 
     row_2 => 
     array 
      'Id' => string '112832' 
      'Status' => string '1' 

我想「natsort」第一重點「list_XX」,使其與0,1,2開始,而不是.. 10,11,12,13,0,1,2,3,...

我玩過array_multisort,但我似乎不能 設置正確的參數,使它做我想要的,如果它甚至能夠做到這一點。

有什麼建議嗎?

+0

能否請您清除您更多的點和後幾原始數據清楚地理解你的問題... –

+1

看看http://stackoverflow.comco米/一/ 12846109/452265 – Jay

回答

0

假設你的數組是這樣的:

$array = [ 
    'list_11' => 
    [ 
     'row_0' => 
     [ 
      'Id' => '118579', 
      'Status' => '3' 
     ], 
     'row_1' => 
     [ 
      'Id' => '117662', 
      'Status' => '2' 
     ], 
     'row_2' => 
     [ 
      'Id' => '117662', 
      'Status' => '2' 
     ] 
    ], 
    'list_10' => 
    [ 
     'row_0' => 
     [ 
      'Id' => '112564', 
      'Status' => '2' 
     ], 
     'row_1' => 
     [ 
      'Id' => '153622', 
      'Status' => '3' 
     ], 
     'row_2' => 
     [ 
      'Id' => '112832', 
      'Status' => '1' 
      ] 
    ], 
     'list_1' => 
    [ 
     'row_0' => 
     [ 
      'Id' => '32323232', 
      'Status' => '3' 
     ], 
     'row_1' => 
     [ 
      'Id' => '2353333', 
      'Status' => '2' 
     ], 
     'row_2' => 
     [ 
      'Id' => '117662', 
      'Status' => '2' 
     ] 
    ] 
]; 

使用array_multisort

$sort = []; 
foreach($array as $el=>$val){ 
    $sort[] = $el; 
} 
array_multisort($array,SORT_NUMERIC,$sort,SORT_NATURAL); 
var_dump($array); 

會打印:

array(3) { 
    ["list_1"]=> 
    array(3) { 
    ["row_0"]=> 
    array(2) { 
     ["Id"]=> 
     string(8) "32323232" 
     ["Status"]=> 
     string(1) "3" 
    } 
    ["row_1"]=> 
    array(2) { 
     ["Id"]=> 
     string(7) "2353333" 
     ["Status"]=> 
     string(1) "2" 
    } 
    ["row_2"]=> 
    array(2) { 
     ["Id"]=> 
     string(6) "117662" 
     ["Status"]=> 
     string(1) "2" 
    } 
    } 
    ["list_10"]=> 
    array(3) { 
    ["row_0"]=> 
    array(2) { 
     ["Id"]=> 
     string(6) "112564" 
     ["Status"]=> 
     string(1) "2" 
    } 
    ["row_1"]=> 
    array(2) { 
     ["Id"]=> 
     string(6) "153622" 
     ["Status"]=> 
     string(1) "3" 
    } 
    ["row_2"]=> 
    array(2) { 
     ["Id"]=> 
     string(6) "112832" 
     ["Status"]=> 
     string(1) "1" 
    } 
    } 
    ["list_11"]=> 
    array(3) { 
    ["row_0"]=> 
    array(2) { 
     ["Id"]=> 
     string(6) "118579" 
     ["Status"]=> 
     string(1) "3" 
    } 
    ["row_1"]=> 
    array(2) { 
     ["Id"]=> 
     string(6) "117662" 
     ["Status"]=> 
     string(1) "2" 
    } 
    ["row_2"]=> 
    array(2) { 
     ["Id"]=> 
     string(6) "117662" 
     ["Status"]=> 
     string(1) "2" 
    } 
    } 
} 
+1

作品像一個魅力的感謝 – user3665396