根据出现次数对数组进行排序

时间:2011-01-13 18:01:12

标签: php multidimensional-array

如何根据键的值出现对数组进行排序,然后返回唯一的数组(已排序)。请考虑以下示例:

$broken_links[] = array("page_id" => 1, "reported_by" => "xyz");
$broken_links[] = array("page_id" => 2, "reported_by" => "xyz");
$broken_links[] = array("page_id" => 1, "reported_by" => "xyz");
$broken_links[] = array("page_id" => 3, "reported_by" => "xyz");
$broken_links[] = array("page_id" => 1, "reported_by" => "xyz");
$broken_links[] = array("page_id" => 10, "reported_by" => "xyz");
$broken_links[] = array("page_id" => 2, "reported_by" => "xyz");

也可以将排序的新排序的唯一数组以某种方式包含出现,以便最终的数组为:

$broken_links[] = array("page_id" => 1, "reported_by" => "xyz", "reported_times" => 3);

3 个答案:

答案 0 :(得分:1)

根据page_id

排序已损坏的链接
function sort_broken_links($a, $b) {
    return $a["page_id"] - $b["page_id"];
}

usort($broken_links, 'sort_broken_links');

我想你可以使用PHP的一些数组函数,如array_unique()array_walk()来修改排序数组以包含reported_times。但为了简单起见,我将创建一个包含每个断开链接记录频率的数组(按上面排序):

$freq = array();

foreach ($broken_links as $link) {
    if (!isset($freq[$link["page_id"]])) {
        $freq[$link["page_id"]] = $link;
        $freq[$link["page_id"]]["reported_times"] = 0;
    }

    $freq[$link["page_id"]]["reported_times"]++;
}

答案 1 :(得分:1)

可以使用array_multisort进行高级数组排序,尤其是使用带有数组的第二个arg

我认为你是第三个例子的情况,还有一个元素,你不想要一个简单的排序,而是按出现次数排序。这里,如果你可以有第二个包含page_id键的数组,但是按出现次数排序,那么你将获得整个数组按ocncences排序。

所以问题是让列表base_id按出现次数排序。这应该是:array_count_values后跟一个简单的排序。

编辑:示例:实际上我们不需要排序,但我们需要提供完整的索引。

$broken_links[] = array("page_id" => 1, "reported_by" => "xyz1");
$broken_links[] = array("page_id" => 2, "reported_by" => "xyz2");
$broken_links[] = array("page_id" => 1, "reported_by" => "xyz3");
$broken_links[] = array("page_id" => 3, "reported_by" => "xyz4");
$broken_links[] = array("page_id" => 55, "reported_by" => "foo");
$broken_links[] = array("page_id" => 1, "reported_by" => "xyz5");
$broken_links[] = array("page_id" => 10, "reported_by" => "xyz6");
$broken_links[] = array("page_id" => 2, "reported_by" => "xyz7");
$broken_links[] = array("page_id" => 55, "reported_by" => "foo");
$broken_links[] = array("page_id" => 55, "reported_by" => "foo");

$pages=array();
foreach ($broken_links as $key => $row) {
    $pages[$key]  = $row['page_id'];
}
//echo "page_id list:\n";print_r($pages);
$pagesidxshort=array_count_values($pages);
//echo "page_id occurences:\n"; print_r($pagesidxshort);
foreach ($pages as $key => $val) {
    $pagesidx[$key]=$pagesidxshort[ $pages[$key] ];
}
//echo "page_id to sort by occurence:\n";print_r($pagesidx);
// here broken links is sorted with same sort as $pagesidx
array_multisort($pagesidx,SORT_DESC,$broken_links);
//echo "Our initial array sorted by occurence of page_id:\n";//print_r($broken_links);

输出(取消注释调试)

page_id list:
Array
    [0] => 1
    [1] => 2
    [2] => 1
    [3] => 3
    [4] => 55
    [5] => 1
    [6] => 10
    [7] => 2
    [8] => 55
    [9] => 55
page_id occurences:
Array
    [1] => 3
    [2] => 2
    [3] => 1
    [55] => 3
    [10] => 1
page_id to sort by occurence:
Array
    [0] => 3
    [1] => 2
    [2] => 3
    [3] => 1
    [4] => 3
    [5] => 3
    [6] => 1
    [7] => 2
    [8] => 3
    [9] => 3
Our initial array sorted by occurence of page_id:
Array
    [0] => Array [page_id] => 1  [reported_by] => xyz1
    [1] => Array [page_id] => 1  [reported_by] => xyz3
    [2] => Array [page_id] => 1  [reported_by] => xyz5
    [3] => Array [page_id] => 55 [reported_by] => foo
    [4] => Array [page_id] => 55 [reported_by] => fo
    [5] => Array [page_id] => 55 [reported_by] => foo
    [6] => Array [page_id] => 2  [reported_by] => xyz2
    [7] => Array [page_id] => 2  [reported_by] => xyz7
    [8] => Array [page_id] => 3  [reported_by] => xyz4
    [9] => Array [page_id] => 10 [reported_by] => xyz6

答案 2 :(得分:0)

function countErrors(array $arr) {
    $new_arr = array();

    foreach ($arr as $val) {
        if (!array_key_exists($val['page_id'], $new_arr)) {
            $new_arr[$val['page_id']] = $val;
            $new_arr[$val['page_id']]['reported_times'] = 1;
        }
        else {
            $new_arr[$val['page_id']]['reported_times']++;
        }
    }

    usort($new_arr, 'sortErrors');

    return $new_arr;
}

function sortErrors($a, $b) {
    return $a['reported_times'] < $b['reported_times'];
}


$broken_links[] = array("page_id" => 1, "reported_by" => "xyz");
$broken_links[] = array("page_id" => 2, "reported_by" => "xyz");
$broken_links[] = array("page_id" => 1, "reported_by" => "xyz");
$broken_links[] = array("page_id" => 3, "reported_by" => "xyz");
$broken_links[] = array("page_id" => 1, "reported_by" => "xyz");
$broken_links[] = array("page_id" => 10, "reported_by" => "xyz");
$broken_links[] = array("page_id" => 2, "reported_by" => "xyz");


$fixed = countErrors($broken_links);