dsf12313 2015-02-08 11:23
浏览 115
已采纳

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

Tough to explain so here's an example:

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

I would like to sort strings by languageCode value, by defining the order:

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" => "您好"),
);

There is magicStringOrder?

I now know that it is a simple problem to solve with a few loops. I would need a very fast function (it is called many times, with large array)

  • 写回答

2条回答 默认 最新

  • dongza1708 2015-02-08 12:41
    关注

    Another idea without php sort functions:

    // 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 (link expires soon)

    $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")));
    

    outputs to:

    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 can also be empty. If so, the array is just reindexed. Arrays with values, that don't exist in $magic_order are shifted down and order of those is maintained.

    Not sure, if that's exactly what you need.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?