【问题标题】:Best way to sort multidimensionnal array对多维数组进行排序的最佳方法
【发布时间】:2014-09-16 03:44:29
【问题描述】:

我有一个像这个例子一样的多维数组

$arr = array (
      range(9,4),
      range(8,0),
      range(2,7),
      range(-1,17)
   );

显示其内容后,我得到了这个

9 8 7 6 5 4 
8 7 6 5 4 3 2 1 0 
2 3 4 5 6 7 
-1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 

现在我想要得到的结果是这样的

-1 0 0 1 1 2 
2 2 3 3 3 4 4 4 4 
5 5 5 5 6 6 
6 6 7 7 7 7 8 8 8 9 9 10 11 12 13 14 15 16 17

我定义了一个对数组进行排序并返回我需要的结果的函数,但我想知道是否有一种使用预定义函数或简单快速算法的简单方法来完成它

这是我的功能

function sort_multi($arr)
{
   $length = 0;
   foreach ($arr as $value) 
     $length += count($value);

   $sorted = false;
   while(!$sorted)
   {
      $x = 0;
      $y = 0;
      $sorted = true;
      while(($x+$y)<($length-1) && isset($arr[$x][$y]))
      {
         $new_x = isset($arr[$x][$y+1])?$x:($x+1);
         $new_y = isset($arr[$x][$y+1])?($y+1):0;
         if(($new_x+$new_y)<$length && isset($arr[$new_x][$new_y]))
            if($arr[$x][$y] > $arr[$new_x][$new_y])
            {
               perm($arr[$x][$y], $arr[$new_x][$new_y]);
               $sorted=false;
            }
         $x = $new_x;
         $y = $new_y;
      }
   }

   return $arr;
}

perm的定义是

function perm(&$a,&$b)
{
   $inter = $a;
   $a = $b;
   $b = $inter;
}

【问题讨论】:

  • 结果应该是二维数组还是一维数组?
  • 第一个结果是在对数组进行排序之前,第二个是我想要在排序之后得到的结果......我的函数做得正确,我只需要知道是否有更好的方法做吧
  • 那么结果数组应该是2d(二维)还是1d?
  • 示例中的两个维度

标签: php arrays algorithm multidimensional-array


【解决方案1】:

我不知道哪种方法最好。但是你可以蛮力做到:Sample Output

$arr = array (
    range(9,4),
    range(8,0),
    range(2,7),
    range(-1,17),
);

// simple checking of number of items per batch (since this is sort of like array_chunk, but jagged)
$chunks = array_map(function($batch){ return count($batch); }, $arr);
foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($arr)) as $v) {
    $values[] = $v; // simple flattening
}
sort($values); // sort ascending (prepping)
foreach($chunks as $key => $batch) {
    // number of items in a batch
    // simple cutting (i can't find the function for this, i think there is)
    for($x = 0; $x < $batch; $x++) {
        $final[$key][] = $values[$x];
        unset($values[$x]);
    }
    $values = array_values($values); // reindex again (resetter)
}

echo '<pre>';
print_r($final);

【讨论】:

    【解决方案2】:

    我通常更喜欢冒泡排序,因为最长它会像蛮力一样。不过通常它的效率会稍微高一些。

    【讨论】:

      【解决方案3】:

      我认为 array_walk_recursive 可以很好地处理这样的事情。

      // Fill a temp array with all the values from the multidimensional array...
      $temp = array();
      array_walk_recursive($arr, function($item) use (&$temp) {
          $temp[] = $item;
      });
      
      sort($temp); // sort it...
      
      // refill the multidimensional array with the sorted values.
      array_walk_recursive($arr, function(&$item) use (&$temp) {
          $item = array_shift($temp);
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-09-07
        • 2017-05-23
        • 1970-01-01
        • 2013-07-20
        • 1970-01-01
        • 2014-07-10
        相关资源
        最近更新 更多