根据纯数组的顺序对多维数组进行排序


Sorting multidimensional array based on the order of plain array

我有这个数组:

  $routes = array(
  array(
     'code' => 'PZSA',
     'name' => 'PLaza san antonio',
  ),
  array(
     'code' => 'AVAD',
     'name' => 'Av de asturias',
  ),
  array(
     'code' => 'AVAR',
     'name' => 'Av simon nieto',
  )
  );

我想根据下一个键对其进行排序:

$target = array('AVAD', 'AVAR', 'PZSA');

所以排序后的数组将是:

Array
(
[0] => Array
    (
        [code] => AVAD
        [name] => Av de asturias
    )
[1] => Array
    (
        [code] => AVAR
        [name] => Av simon nieto
    )
[2] => Array
    (
        [code] => PZSA
        [name] => PLaza san antonio
    )
)

我已经尝试过这个并且它有效,但我认为对于这个简单的事情来说代码太多了。还有其他选择吗?谢谢。

  function _sort($array, $orderArray) {
      $ordered = array();
      foreach ($orderArray as $key) {
        $ordered[] = find($key, $array);
      }
      return $ordered;
   }
   function find($code, $routes) {
      foreach ($routes as $key => $value) {
         if ($routes[$key]['code'] == $code) {
            return $routes[$key];
         }
      }
   }
$sorted = _sort($routes, $target);
$target_flip = array_flip($target);
usort($routes, function($a, $b) use ($target_flip){
    return ($target_flip[$a['code']] < $target_flip[$b['code']]) ? -1 : 1;
});

演示:

  • http://codepad.viper-7.com/DGDbAr

文档:

  • http://php.net/manual/en/function.array-flip.php
  • http://php.net/manual/en/function.usort.php
  • http://php.net/manual/en/functions.anonymous.php

与其对每个元素进行本质上的线性搜索,不如通过代码重新索引原始数组

// create the index
$indexed = array();
foreach($routes as $route) {
    $indexed[$route['code']] = $route;
}
// add each target to a new sorted array in order
$sorted = array();
foreach($target as $code) {
    $sorted[] = $indexed[$code];
}

一个更通用的解决方案,您可以在其中设置要排序的键,它应该适用于任何多维数组。

//key to sort by
$sortBy = 'code';
$sorted = array();
foreach($routes as $route) {
  foreach($route as $key => $value) {
    if(!isset($sorted[$key])) {
      $sorted[$key] = array();
    }
    $sorted[$key][] = $value;
  }
}
array_multisort($sorted[$sortBy], SORT_ASC, $routes); 
print_r($routes)
这就是

array_multisort如果:

foreach ($routes as $key => $row) {
    $code[$key]  = $row['code'];
    $name[$key] = $row['name'];
}
array_multisort($code, SORT_ASC, $name, SORT_ASC, $routes);
print_r( $routes );

这样你甚至不需要第二个数组!

实际上,在您的情况下,您只需要对代码进行排序,因此这将解决问题:

foreach ($routes as $key => $row) {
    $code[$key]  = $row['code'];
}
array_multisort($code, SORT_ASC, $routes);

除了已经提到的答案(彼得除外(,您可以将逻辑与 uasort() 一起使用。您首先必须定义一个比较函数(将$a与$b进行比较(,并使用值 -101 构建逻辑,以决定比较的行是应该在之前、之后还是保持不变。

// This sorts simply by alphabetic order
function reindex( $a, $b )
{
    // Here we grab the values of the 'code' keys from within the array.
    $val1 = $a['code'];
    $val2 = $b['code'];
    // Compare string alphabetically
    if( $val1 > $val2 ) {
        return 1;
    } elseif( $val1 < $val2 ) {
        return -1;
    } else {
        return 0;
    }
}
// Call it like this:
uasort( $routes, 'reindex' );
print_r( $routes );

当然,这只是一个小例子,如果您尝试按字母顺序索引它们。如果您需要它按一组精确的键进行排序,而不是按字母顺序排序,那么它可能有点棘手,因为它不接受任何其他参数。

PHP uasort(( 参考