按特定顺序对多维数组排序

时间:2019-12-03 07:48:02

标签: php sorting multidimensional-array

im试图按特定顺序对多维数组进行排序。我的数组看起来像这样:

array (size=4)
  '1574764780' => 
    array (size=4)
      0 => 
        array (size=3)
          'name' => string 'x' (length=1)
          'nr' => string '5' (length=1)
          'color' => string 'red' (length=3)
      1 => 
        array (size=3)
          'name' => string 'y' (length=1)
          'nr' => string '6' (length=1)
          'color' => string 'yellow' (length=6)
      2 => 
        array (size=3)
          'name' => string 'z' (length=1)
          'nr' => string '4' (length=1)
          'color' => string 'blue' (length=4)
      3 => 
        array (size=3)
          'name' => string 'a' (length=1)
          'nr' => string '2' (length=1)
          'color' => string 'green' (length=5)
  '36879010280' => 
           ...

我需要对它进行排序,使颜色为红色的数组排在首位,绿色为第二排,依此类推:关键颜色的顺序应类似于:

red,green,blue,yellow

但是每个数组中可能没有绿色。然后应该是这样:

red,blue,yellow

输出应为:

array (size=4)
  '1574764780' => 
    array (size=4)
      0 => 
        array (size=3)
          'name' => string 'x' (length=1)
          'nr' => string '5' (length=1)
          'color' => string 'red' (length=3)
      1 => 
        array (size=3)
          'name' => string 'a' (length=1)
          'nr' => string '2' (length=1)
          'color' => string 'green' (length=5)
      2 => 
        array (size=3)
          'name' => string 'z' (length=1)
          'nr' => string '4' (length=1)
          'color' => string 'blue' (length=4)
      3 => 
        array (size=3)
          'name' => string 'y' (length=1)
          'nr' => string '6' (length=1)
          'color' => string 'yellow' (length=6)
  '36879010280' => 
           ...

我一直在搜索数小时,但是找不到一个很好的方法来做到这一点。 非常感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

我做了类似的事情,但是我只有一个维数组。希望对您有帮助

usort($ar, function ($a, $b) use ($order) {
    $pos_a = array_search($a['color'], $order);
    $pos_b = array_search($b['color'], $order);
    return $pos_a - $pos_b;
});

答案 1 :(得分:0)

您可以尝试这样做。代码不漂亮,但是您可以参考它。希望对您有所帮助。

$arr = array(
  '1574764780' => array(
        0 => array(
            'name' =>  'x',
            'nr' =>  '5',
            'color' =>  'red'
        ),
        1 => array (
            'name' =>  'a',
            'nr' =>  '2',
            'color' =>  'green'),
        2 => array (
            'name' =>  'z',
            'nr' =>  '4',
            'color' =>  'blue'),
        3 => array (
            'name' =>  'y',
            'nr' =>  '6',
            'color' =>  'yellow')
    ),
    '1574764781' => array(
        0 => array(
              'name' =>  'x',
              'nr' =>  '5',
              'color' =>  'blue'
        ),
        1 => array (
            'name' =>  'a',
            'nr' =>  '2',
            'color' =>  'yellow'),
        2 => array (
            'name' =>  'z',
            'nr' =>  '4',
            'color' =>  'red'),
        3 => array (
            'name' =>  'y',
            'nr' =>  '6',
            'color' =>  'green')
    )

);

$color_order = array(
    'red' => 1,
    'green' => 2,
    'blue' => 3,
    'yellow' => 4
);

$new_array = array_map(function($items) use ($color_order){
    $ordered_array = array();
    foreach($items as $item) {
        $item['order'] = $color_order[$item['color']];
        $ordered_array[] = $item;
    }
    usort($ordered_array, "compare"); //sort the array by color order
    return $ordered_array; // the sorted array
}, $arr);


function compare($a, $b){
    if ($a == $b)
        return 0;
    return ($a['order'] < $b['order']) ? -1 : 1;
}

print_r($new_array);