如何按PHP中的键值对关联数组进行排序

时间:2015-02-08 11:23:17

标签: php arrays

很难解释这里的例子:

$strings = array(
   array("languageCode" => "ES",  "string" => "hola"),
   array("languageCode" => "EN",  "string" => "hello"),
   array("languageCode" => "IT",  "string" => "ciao"),
   array("languageCode" => "CHS", "string" => "您好"),
);

我想通过定义顺序来按languageCode值对字符串进行排序:

function magicStringOrder(array $strings, array $languageCodeOrder){
    // ....
    return $strings;
}

$strings = magicStringOrder($strings, array('EN', 'IT') );

$strings = array(
   array("languageCode" => "EN",  "string" => "hello"),
   array("languageCode" => "IT",  "string" => "ciao"),
   array("languageCode" => "ES",  "string" => "hola"),
   array("languageCode" => "CHS", "string" => "您好"),
);

magicStringOrder

我现在知道用一些循环解决这个问题很简单。我需要一个非常快的函数(它被称为很多次,有大数组)

2 个答案:

答案 0 :(得分:2)

没有php排序功能的另一个想法:

// sort by cmp $arr[$magic_key] <-> arr $magic_order
function magicSort($arr, $magic_key="", $magic_order = array())
{
  $sorted = array();
  foreach($magic_order AS $v) {
    foreach($arr AS $k2 => $v2) {
      if($v===$v2[$magic_key]) {
        $sorted[] = $v2;
        unset($arr[$k2]);
      }
    }
  }

  // attach what's left
  return array_merge($sorted, $arr);
}

Test it at eval.in链接即将过期

$strings = array(
   array("languageCode" => "ES",  "string" => "hola"),
   array("languageCode" => "EN",  "string" => "hello"),
   array("languageCode" => "IT",  "string" => "ciao"),
   array("languageCode" => "CHS", "string" => "??"),
);

print_r(magicSort($strings, "languageCode", array("EN", "IT")));

输出到

Array
(
    [0] => Array
        (
            [languageCode] => EN
            [string] => hello
        )

    [1] => Array
        (
            [languageCode] => IT
            [string] => ciao
        )

    [2] => Array
        (
            [languageCode] => ES
            [string] => hola
        )

    [3] => Array
        (
            [languageCode] => CHS
            [string] => 您好
        )

)

$magic_order也可以为空。如果是这样,阵列只是重新编制索引。 <{1}}中不存在的具有值的数组将向下移动,并保持其顺序。

不确定,如果这正是您所需要的。

答案 1 :(得分:1)

如果给一种语言赋予权重不是问题,那么排序会变得更容易,因此更快。

您可以使用asort根据$languageCodeOrder存储的权重对数组进行排序。如果$languageCodeOrder其权重中不存在的语言变为0并且朝向有序数组的末尾。

阵列的最终顺序将从高重量到低重量。也就是说:如果EN的权重为1,它将在具有2的IT之后出现在数组中。

$strings = array(
    array("languageCode" => "ES", "string" => "hola"),
    array("languageCode" => "EN", "string" => "hello"),
    array("languageCode" => "IT", "string" => "ciao"),
    array("languageCode" => "CHS", "string" => "您好"),
);

function magicStringOrder(array $strings, array $languageCodeOrder) {
    uasort($strings, function($a,$b) use ($languageCodeOrder) {
        $val_a = (isset($languageCodeOrder[$a["languageCode"]])) ? $languageCodeOrder[$a["languageCode"]] : 0;
        $val_b = (isset($languageCodeOrder[$b["languageCode"]])) ? $languageCodeOrder[$b["languageCode"]] : 0;

        return $val_b - $val_a;
    });
    return $strings;
}

$strings_ordered = magicStringOrder($strings, array('EN' => 1, 'IT' => 2, 'ES' => 3, 'CHS' => 4));

print_r($strings_ordered);

如果阵列很大,最好依靠PHP排序算法而不是尝试实现自定义算法。

随意修改算法中的任何内容(顺序或其他)。