使用字符串值自定义数组排序

时间:2014-04-28 22:18:30

标签: php arrays sorting

假设我有这个数组:

$array = array(
    array("id" => 7867867, "animal" => "Dog"),
    array("id" => 3452342, "animal" => "Lion"),
    array("id" => 1231233, "animal" => "Lion"),
    array("id" => 5867867, "animal" => "Dog"),
    array("id" => 1111111, "animal" => "Zeebra"),
    array("id" => 2222222, "animal" => "Cat"),
    array("id" => 3333333, "animal" => "Cat"),
    array("id" => 4444444, "animal" => "Zeebra")
);

现在我一直在尝试使用php排序函数来根据特定规则对其进行排序(不是按字母顺序排列)

客户希望此信息按“Lion first,Dog second,Zeebra third,Cat 4th”排序。

这样的事情:

$array = array(
    array("id" => 3452342, "animal" => "Lion"),
    array("id" => 1231233, "animal" => "Lion"),
    array("id" => 7867867, "animal" => "Dog"),
    array("id" => 5867867, "animal" => "Dog"),
    array("id" => 4444444, "animal" => "Zeebra"),
    array("id" => 1111111, "animal" => "Zeebra"),
    array("id" => 2222222, "animal" => "Cat"),
    array("id" => 3333333, "animal" => "Cat"),
);

数组将使用“动物”值进行排序,并且将基于预先确定的规则。

我试图找出php排序函数,但我只能按照字母顺序或数字顺序对数组进行排序。

我已经开始工作的是一个if语句和循环块,我想尽快摆脱那些慢速代码。

5 个答案:

答案 0 :(得分:6)

使用有关使用usort的Jonathan建议,您可以在单独的函数中定义自定义规则以进行排序,例如:

function getAnimalValue($animal) {
    switch($animal) {
        case 'Lion':
            return 1;
        case 'Dog':
            return 2;
        case 'Zeebra':
            return 3;
        case 'Cat':
            return 4;
    }
    return 0;
}

然后,实现自己的比较功能:

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

最后,使用compare函数调用usort:

usort($array, "compare");

答案 1 :(得分:1)

检查我们。继承人参考:

http://www.php.net/manual/en/function.usort.php

示例:

function cmp($a, $b) {
    $order=array("Lion","Dog","Zebra","Cat");
    if ($a["animal"] == $b["animal"]) {
        return 0;
    }
    return (array_search($a["animal"],$order) < array_search($b["animal"],$order)) ? -1 : 1;
}

$array = array(
    array("id" => 7867867, "animal" => "Dog"),
    array("id" => 3452342, "animal" => "Lion"),
    array("id" => 1231233, "animal" => "Lion"),
    array("id" => 5867867, "animal" => "Dog"),
    array("id" => 1111111, "animal" => "Zebra"),
    array("id" => 2222222, "animal" => "Cat"),
    array("id" => 3333333, "animal" => "Cat"),
    array("id" => 4444444, "animal" => "Zebra")
);

$mySortedArray=usort($array, "cmp");

答案 2 :(得分:0)

这有点丑陋,但确实有效。

// Set our rules, the key is the order
$rules = array(0=>"Dog",1=>"Lion",2=>"Zeebra",3=>"Cat");

// Our array
$array = array(
    array("id" => 7867867, "animal" => "Dog"),
    array("id" => 3452342, "animal" => "Lion"),
    array("id" => 1231233, "animal" => "Lion"),
    array("id" => 5867867, "animal" => "Dog"),
    array("id" => 1111111, "animal" => "Zeebra"),
    array("id" => 2222222, "animal" => "Cat"),
    array("id" => 3333333, "animal" => "Cat"),
    array("id" => 4444444, "animal" => "Zeebra")
);

// Split each animal into a arrays per animal
foreach( $array as $item ){
    $animals[ $item['animal'] ][] = $item;
}

// Loop our rules (dogs, then lions, etc) and put our mini arrays in a final sorted_array.
foreach( $rules as $animal_order => $animal_name ){
    foreach( $animals[$animal_name] as $animal ){
        $sorted_animals[] = $animal;
    }

}

print_r($sorted_animals);

答案 3 :(得分:0)

尝试:

function sortByAnimals($array, $animalOrderArray){
  foreach($animalOrderArray as $v){
    $aa[$v] = array();
  }
  foreach($array as $a){
    foreach($aa as $i => $v){
      if(preg_match("/^{$a['animal']}$/i", $i)){
        array_push($aa[$i], $a);
        break;
      }
    }
  }
  foreach($aa as $a){
    foreach($a as $v){
      $r[] = $v;
    }
  }
  return $r;
}
$resArray = sortByAnimals($array, array('lion', 'dog', 'zeebra', 'cat'));

答案 4 :(得分:-2)

PHP7提供了“ spaceship operator”,使排序变得非常简单。

以下所有代码段都将利用查找数组来确定自定义排序顺序。

while(true)

代码:(Demo

$priorities = array_flip(['Lion', 'Dog', 'Zeebra', 'Cat']);

PHP7.4 +代码:(Demo

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

您的问题和样本数据表明,您在查找数组中总会承认所有动物。如果并非总是如此,那么您将需要在自定义函数中进行usort($array, fn($a, $b) => $priorities[$a['animal']] <=> $priorities[$b['animal']]); 调用,并确定是否要将丢失的动物放在列表的前面或后面。

以上两个代码段都提供相同的输出:

isset()