Which algorithm to use for searching unique element in array?
For example array is:
$arr = [
["date" => "2015/04/05",
"country" => "UK",
"number" => 3],
["date" => "2015/04/05",
"country" => "UK",
"number" => 3],
["date" => "2015/04/05",
"country" => "UK",
"number" => 3],
["date" => "2015/04/05",
"country" => "UK",
"number" => 4],
];
So, I need to count how much element with unique number
?
In result I should get the following structure:
["UK" => array("total" => "2")]
Where total
is number of unique number
in array.
I tried as:
$output = [];
foreach ($arr as $k => $value) {
$output[
$value[
"country"
]][$value["date"]][$value["number"]
] = true;
}
foreach ($output as $k => $v) {
foreach ($v as $c => $t) {
$arr[$k][$c] = count($t);
}
}
But this code looks so awful, with loops. Also I think there is a trivial algorithm to resolve such issues.