<?php
$array = [
['customerName' => "John Dunk", 'customerPhone' => 5555555555, 'Difference' => 125.0],
['customerName' => "John Dunk", 'customerPhone' => 5555555555, 'Difference' => 192.0],
['customerName' => "James Morle", 'customerPhone' => 111111111, 'Difference' => 145.0],
['customerName' => "James Morle", 'customerPhone' => 111111111, 'Difference' => 114.0],
];
$result = [];
foreach ($array as $row) {
if (!isset($ref[$row['customerName']])) {
$ref[$row['customerName']] = $row;
$result[] =& $ref[$row['customerName']];
} elseif ($ref[$row['customerName']]['Difference'] > $row['Difference']) {
$ref[$row['customerName']]['customerPhone'] = $row['customerPhone'];
$ref[$row['customerName']]['Difference'] = $row['Difference'];
}
}
var_export($result);
- Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
- array (
0 =>
array (
'customerName' => 'John Dunk',
'customerPhone' => 5555555555,
'Difference' => 125.0,
),
1 =>
array (
'customerName' => 'James Morle',
'customerPhone' => 111111111,
'Difference' => 114.0,
),
)
preferences:
56.56 ms | 402 KiB | 62 Q