Php 根据出现情况对数组进行排序

Php 根据出现情况对数组进行排序,php,multidimensional-array,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" => 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);

要根据
页面id
对断开的链接进行排序:

function sort_broken_links($a, $b) {
    return $a["page_id"] - $b["page_id"];
}

usort($broken_links, 'sort_broken_links');
我想您可以使用PHP的一些数组函数,如
array\u unique()
array\u walk()
来修改排序后的数组,以包含
报告的时间。但为了简单起见,我将创建一个数组,其中包含每个断开链接记录的频率(如上所述排序):


可以使用进行高级数组排序,特别是通过使用第二个arg和数组进行排序

我认为您是在第三个示例中,如果还有一个元素,您不希望进行简单的排序,而是希望按照出现次数进行排序。在这里,如果您可以有第二个数组,其中包含page_id键,但按事件排序,那么您将获得按ocurence排序的整个数组

因此,问题是如何获得按事件排序的列表基id。这应该是可用的:后跟一个简单的排序

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

$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);
输出(未注释调试)

函数计数器(数组$arr){
$new_arr=array();
外汇($arr作为$val){
如果(!array_key_存在($val['page_id',$new_arr)){
$new_arr[$val['page_id']]=$val;
$new_arr[$val['page_id']['reported_times']=1;
}
否则{
$new_arr[$val['page_id']['reported_times']+];
}
}
usort($new_arr,'sortErrors');
返回$new_arr;
}
功能分类错误($a,$b){
返回$a['reported_times']<$b['reported_times'];
}
$breaked_links[]=数组(“page_id”=>1,“由”=>xyz报告”);
$breaked_links[]=数组(“page_id”=>2,由“=>xyz”报告);
$breaked_links[]=数组(“page_id”=>1,“由”=>xyz报告”);
$breaked_links[]=数组(“page_id”=>3,“由”=>xyz报告”);
$breaked_links[]=数组(“page_id”=>1,“由”=>xyz报告”);
$breaked_links[]=array(“page_id”=>10,“由”=>xyz报告”);
$breaked_links[]=数组(“page_id”=>2,由“=>xyz”报告);
$fixed=计数器错误($breaked_links);

在第二种情况下,您可能会修改数组值,或者生成一个新的数组,这不算排序。自定义排序的好方法,不知道php中有一个内置函数(usort()),这会提供初始数组的恢复,按精度排序(这可能是需要的)。但是如果“reported_by”值不总是相同的话,你会删除重要的数据。谢谢你提供了一个完整的解决方案,这正是我想要的。如果需要,可以将“reported_by”添加到新阵列内的子阵列中
$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
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);