如何根据键的值出现对数组进行排序,然后返回唯一的数组(排序)。考虑以下示例:
$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");
noso也有可能以某种方式分类的新排序唯一数组包括事件,以便最终数组为:
$broken_links[] = array("page_id" => 1, "reported_by" => "xyz", "reported_times" => 3);
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"]++;
}
array_multisort进行adadanced Array排序,尤其是通过使用ARRAY的第二个arg。 我认为您是第三个示例,还有一个元素,您不想简单,而要按照出现的数量进行排序。在这里,如果您可以将一个包含page_id键的第二个数组,但按照事件进行排序,那么您将获得由Ocurences排序的整个数组。 因此,问题是通过出现列表base_id列表。这应该提供以下内容:array_count_values
之后是简单的。 Edit:示例:输出(带有调试的不计费)
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);
可以通过参考修改新列来在一个循环中完成行过滤和发生计数。然后可以通过