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 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用