чтобы из этого
Array
(
[0] => Array
(
[id] => 367
[user] => text
)
[1] => Array
(
[id] => 370
[user] => text
)
[2] => Array
(
[id] => 432
[user] => text1
)
[3] => Array
(
[id] => 234
[user] => text33
)
[4] => Array
(
[id] => 264
[user] => text33
)
)
Array
(
[0] => Array
(
[cout или тот же id] = 2
[user] => text
)
[2] => Array
(
[id] => 432
[user] => text1
)
[3] => Array
(
[cout или тот же id] = 2
[user] => text33
)
)
function mergeCount($arr) {
$keys = [];
foreach($arr as $item) {
$id = $item['id'];
$user = $item['user'];
if( isset( $keys[ $user])) {
$keys[ $user]['count']++;
} else {
$keys[$user] = [
'id' => $id,
'count' => 1
];
}
}
$result = [];
foreach($keys as $user=>$item) {
$result[] = [
'id' => $item['count'] > 1 ? $item['count'] : $item['id'],
'user' => $user
];
}
return $result;
}
$data = [
['id'=>367, 'user'=>'text'],
['id'=>370, 'user'=>'text'],
['id'=>432, 'user'=>'text1'],
['id'=>234, 'user'=>'text33'],
['id'=>264, 'user'=>'text33'],
];
$a = mergeCount($data);
print_r($a);
Array
(
[0] => Array
(
[id] => 2
[user] => text
)
[1] => Array
(
[id] => 432
[user] => text1
)
[2] => Array
(
[id] => 2
[user] => text33
)
)