我如何排序这个数组的值“order”键?

尽管这些值目前是连续的,但它们并不总是连续的。

Array
(
    [0] => Array
        (
            [hashtag] => a7e87329b5eab8578f4f1098a152d6f4
            [title] => Flower
            [order] => 3
        )

    [1] => Array
        (
            [hashtag] => b24ce0cd392a5b0b8dedc66c25213594
            [title] => Free
            [order] => 2
        )

    [2] => Array
        (
            [hashtag] => e7d31fc0602fb2ede144d18cdffd816b
            [title] => Ready
            [order] => 1
        )
)

当前回答

让我们面对这个问题:PHP没有一个简单的开箱即用的函数来正确处理每个数组排序场景。

这个例程很直观,这意味着更快的调试和维护:

// Automatic population of the array
$tempArray = array();
$annotations = array();
// ... some code
// SQL $sql retrieves result array $result
// $row[0] is the ID, but is populated out of order (comes from
// multiple selects populating various dimensions for the same DATE
// for example
while($row = mysql_fetch_array($result)) {
    $needle = $row[0];
    arrayIndexes($needle);  // Create a parallel array with IDs only
    $annotations[$needle]['someDimension'] = $row[1]; // Whatever
}
asort($tempArray);
foreach ($tempArray as $arrayKey) {
    $dataInOrder = $annotations[$arrayKey]['someDimension'];
    // .... more code
}

function arrayIndexes ($needle) {
    global $tempArray;
    if (!in_array($needle, $tempArray)) {
        array_push($tempArray, $needle);
    }
}

其他回答

正如公认的答案所说,你可以使用:

usort($myArray, function($a, $b) {
    return $a['order'] <=> $b['order'];
});

如果你需要按多个列排序,那么你会做下面的事情:

usort($myArray, function($a, $b) {
    return [$a['column1'],$a['column2']] <=> [$b['column1'],$b['column2']];
});

这可以扩展到数据中的任意数量的列。这依赖于可以在PHP中直接比较数组的事实。在上面的例子中,数组首先按columnn1排序,然后按column2排序。但是你可以按照列的任何顺序进行排序,例如:

usort($myArray, function($a, $b) {
    return [$a['column2'],$a['column1']] <=> [$b['column2'],$b['column1']];
});

如果需要将一列升序排序,另一列降序排序,则将降序列交换到操作符<=>的另一侧:

usort($myArray, function($a, $b) {
    return [$a['column1'],$b['column2']] <=> [$b['column1'],$a['column2']];
});

让我们面对这个问题:PHP没有一个简单的开箱即用的函数来正确处理每个数组排序场景。

这个例程很直观,这意味着更快的调试和维护:

// Automatic population of the array
$tempArray = array();
$annotations = array();
// ... some code
// SQL $sql retrieves result array $result
// $row[0] is the ID, but is populated out of order (comes from
// multiple selects populating various dimensions for the same DATE
// for example
while($row = mysql_fetch_array($result)) {
    $needle = $row[0];
    arrayIndexes($needle);  // Create a parallel array with IDs only
    $annotations[$needle]['someDimension'] = $row[1]; // Whatever
}
asort($tempArray);
foreach ($tempArray as $arrayKey) {
    $dataInOrder = $annotations[$arrayKey]['someDimension'];
    // .... more code
}

function arrayIndexes ($needle) {
    global $tempArray;
    if (!in_array($needle, $tempArray)) {
        array_push($tempArray, $needle);
    }
}

这个解决方案适用于usort(),具有易于记忆的多维排序符号。使用了飞船操作符<=>,该操作符可从PHP 7获得。

usort($in,function($a,$b){
  return $a['first']   <=> $b['first']  //first asc
      ?: $a['second']  <=> $b['second'] //second asc
      ?: $b['third']   <=> $a['third']  //third desc (a b swapped!)
      //etc
  ;
});

例子:

$in = [
  ['firstname' => 'Anton', 'surname' => 'Gruber', 'birthdate' => '03.08.1967', 'rank' => 3],
  ['firstname' => 'Anna', 'surname' => 'Egger', 'birthdate' => '04.01.1960', 'rank' => 1],
  ['firstname' => 'Paul', 'surname' => 'Mueller', 'birthdate' => '15.10.1971', 'rank' => 2],
  ['firstname' => 'Marie', 'surname' => 'Schmidt ', 'birthdate' => '24.12.1963', 'rank' => 2],
  ['firstname' => 'Emma', 'surname' => 'Mueller', 'birthdate' => '23.11.1969', 'rank' => 2],
];

第一个任务:按等级asc,姓asc排序

usort($in,function($a,$b){
  return $a['rank']      <=> $b['rank']     //first asc
      ?: $a['surname']   <=> $b['surname']  //second asc
  ;
});

第二项任务:按等级,姓,名排序

usort($in,function($a,$b){
  return $b['rank']      <=> $a['rank']       //first desc
      ?: $a['surname']   <=> $b['surname']    //second asc
      ?: $a['firstname'] <=> $b['firstname']  //third asc
  ;
});

第三个任务:按级别desc,出生日期asc排序

日期不能用这种表示法排序。使用strtotime进行转换。

usort($in,function($a,$b){
  return $b['rank']      <=> $a['rank']       //first desc
      ?: strtotime($a['birthdate']) <=> strtotime($b['birthdate'])    //second asc
  ;
});
 example  with class:
 
 class user
 {
     private $key;

     public function __construct(string $key)
     {
         $this->key = $key;
     }

     public function __invoke($a, $b)
     {
         return $a[$this->key] <=> $b[$this->key];
     }
 }

 $user = [
     ['id' => 1, 'name' => 'Oliver', 'id_card' => 4444],
     ['id' => 3, 'name' => 'Jack', 'id_card' => 5555],
     ['id' => 2, 'name' => 'Harry', 'id_card' => 6666]
 ];

 // sort user by id
 usort($user, new user('id'));
 var_dump($user);

我通常使用usort,并传递我自己的比较函数。在这种情况下,它非常简单:

function compareOrder($a, $b)
{
  return $a['order'] - $b['order'];
}
usort($array, 'compareOrder');

在PHP 7中使用太空船操作符:

usort($array, function($a, $b) {
    return $a['order'] <=> $b['order'];
});