PHP通过第一个键排序multidimesional数组

时间:2015-12-04 11:25:21

标签: php arrays sorting natsort

我有一个这样的数组:

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但我似乎无法 如果它能够做到这一点,设置正确的参数让它做我想做的事。

任何建议?

1 个答案:

答案 0 :(得分: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"
    }
  }
}