我需要在多维数组中计算相同的值并删除重复项。
我的阵列:
$r = [
['a','b'],
['a','b'],
['c','d'],
['c','d'],
['c','d'],
['e','f'],
];
需要输出:
[0] => Array
(
[0] => a
[1] => b
[1] => 2 // Result
)
[1] => Array
(
[0] => c
[1] => d
[1] => 3 // Result
)
[2] => Array
(
[0] => e
[1] => f
[1] => 1 // Result
)
我将非常感谢你的帮助。
答案 0 :(得分:2)
<?php
$r = [
['a','b'],
['a','b'],
['c','d'],
['c','d'],
['c','d'],
['e','f'],
];
foreach($r as $arr)
{
$o[implode(',', $arr)][] = 1;
}
$output = [];
array_walk($o, function($v, $k) use(&$output){
$output[] = array_merge(explode(',', $k), [count($v)]);
});
var_dump($output);
和输出:
array(3) {
[0]=>
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
int(2)
}
[1]=>
array(3) {
[0]=>
string(1) "c"
[1]=>
string(1) "d"
[2]=>
int(3)
}
[2]=>
array(3) {
[0]=>
string(1) "e"
[1]=>
string(1) "f"
[2]=>
int(1)
}
}
答案 1 :(得分:0)
foreach ( $result1 as $key ):
$o[implode(', ', $key)][] = null;
foreach ($o as $key1) {
$g[implode(', ', $key)] = count($key1);
}
endforeach;
print_r($g);