我试图计算并删除重复的数组。要成为一个副本,整个数组将使用键和值与另一个匹配。
Array
(
[0] => Array
(
[name] => Superman
[time] => 60
)
[1] => Array
(
[name] => Superman
[time] => 60
)
[2] => Array
(
[name] => Superman
[time] => 50
)
[3] => Array
(
[name] => Superman
[time] => 40
)
[4] => Array
(
[name] => Superman
[time] => 50
)
[5] => Array
(
[name] => Superman
[time] => 60
)
)
分为:
Array
(
[0] => Array
(
[name] => Superman
[time] => 60
[count] => 3
)
[1] => Array
(
[name] => Superman
[time] => 50
[count] => 2
)
[2] => Array
(
[name] => Superman
[time] => 40
)
)
我遇到了this answer that can remove the duplicates,但我很难看到我如何算他们。
$input = array_map("unserialize", array_unique(array_map("serialize", $input)));
答案 0 :(得分:1)
在这种情况下,您也可以使用array_count_values
。例如:
$values = array(
array('name' => 'Superman', 'time' => 60),
array('name' => 'Superman', 'time' => 60),
array('name' => 'Superman', 'time' => 50),
array('name' => 'Superman', 'time' => 40),
array('name' => 'Superman', 'time' => 50),
array('name' => 'Superman', 'time' => 60),
);
// map out into string then unique them
$uniques = array_map("unserialize", array_unique(array_map("serialize", $values)));
$count = array_count_values(array_map("serialize", $values)); // map out the arrays then get the counts
// then to merge the count
foreach($uniques as &$batch) {
foreach($count as $array => $v) {
if(unserialize($array) == $batch) { // if this particular key count is equal to this unique array, then push the count
$batch['count'] = $v;
}
}
}
echo '<pre>';
print_r($uniques);
答案 1 :(得分:1)
快速而又脏,但您可以获得所要求的数据结构:
$data = array(
array("name" => "Superman", "time" => 60),
array("name" => "Superman", "time" => 60),
array("name" => "Superman", "time" => 50),
array("name" => "Superman", "time" => 40),
array("name" => "Superman", "time" => 50),
array("name" => "Superman", "time" => 60),
);
// count the occurrences
$occurrences = array();
for ($i = 0, $l = count($data); $i < $l; $i++) {
$serialized = serialize($data[$i]);
if (!isset($occurrences[$serialized])) {
$occurrences[$serialized] = 1;
}
else {
$occurrences[$serialized] = $occurrences[$serialized] + 1;
}
}
// get the wanted structure
$uniques = array();
foreach ($occurrences as $serialized => $count) {
$unserialized = unserialize($serialized);
if ($count > 1) {
$unserialized['count'] = $count;
}
$uniques[] = $unserialized;
}
print_r($uniques);
答案 2 :(得分:1)
不像我想的那样紧凑,但它完成了工作:
function unserialize_unique_count($input, $k = 'count') {
$a = [];
foreach ($input as $d) {
$s = serialize($d);
$a[$s] = (isset($a[$s]) ? ($a[$s] + 1) : 1);
}
foreach ($a as $s => $c) {
$a[$s] = unserialize($s) + [ $k => $c ];
}
return array_values($a);
}
$grouped_with_count = unserialize_unique_count($input);
工作原理:第一个循环序列化并计数。第二个独特的合并。为O(n)。
使用方法:将多维数组作为参数#1传递。使用额外的键&#34;计数&#34;持有点数。如果你想让count键不是&#34; count&#34;,那就把第二个参数传递给函数。
答案 3 :(得分:0)
我将时间用作唯一键。
$tmpArr = Array();
$cnt = sizeof($arr);
for($i=0;$i<$cnt;$i++){
$time = $arr[$i]['time'];
if(!is_array($tmpArr[$time])){
$tmpArr[$time] = Array();
$tmpArr[$time]['count'] = 0;
}
$tmpArr[$time]['time'] = $arr[$i]['time'];
$tmpArr[$time]['name'] = $arr[$i]['name'];
$tmpArr[$time]['count'] = $tmpArr[$time]['count'] + 1;
}
print_r($tmpArr);
注意:根据您的要求对代码进行更改