dphphvs496524 2011-05-03 12:32
浏览 91
已采纳

优化多维数组的重新排序和重复删除

I wondering whether anyone has any good ideas on optimizing the following code. I have an multi-dimensional array ($List) as follows:

Array
(
    [0] => Array
    (
        [id] => 1
        [title] => A good read
        [priority] => 10
    )

    [1] => Array
    (
        [id] => 2
        [title] => A bad read
        [priority] => 20

    )

    [2] => Array
    (
        [id] => 3
        [title] => A good read
        [priority] => 10
    )
)

First I'm removing any entries that share the same title (no matter what the other values are) as follows:

$List_new = array();
foreach ($List as $val) {
    $List_new[$val['title']] = $val;    
}
$List = array_values($List_new);

Perfect. Then I'm reordering the array, first by the priority field and then id:

$sort_id = array();
$sort_priority = array();
foreach ($List as $key => $row) {
    $sort_id[$key] = $row['id'];
    $sort_priority[$key] = $row['priority'];
}
array_multisort($sort_priority, SORT_DESC, $sort_id, SORT_DESC, $List);

Both code blocks appear in a loop, hence the clearing of $sort_id and $sort_priority before reordering.

Is there a better way to do this - i.e. use the sorting process to remove duplicate title entries? This code block is being executed in a loop of up to 500,000 records and so any improvement would be welcome!

  • 写回答

1条回答 默认 最新

  • duandou2763 2011-05-03 15:13
    关注

    One loop, but a few extra function calls so I can't tell you how the Big O changes. One thing to note, the padding around numbers must be big enough to prevent overflow i.e. 2 = max 99 priorities and 6 = max 999,999 items.

    $list_titles = array();
    foreach($List as $val) {
        if(isset($list_titles[$val['title']])) continue;
        $list_titles[$val['title']] = true;
        $List_new[str_pad($val['priority'], 2, 0, STR_PAD_LEFT).str_pad($val['id'], 6, 0, STR_PAD_LEFT)] = $val;
    }
    krsort($List_new);
    

    Edit: made some minor modifications.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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