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 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图