Answer the question
In order to leave comments, you need to log in
[[+content_image]]
PHP Associative array, how to filter by largest values?
Good day, there is an associative array:
$arr = [['name' => 'ford', 'count' => 2], ['name' => 'ford', 'count' => 8], ['name' => 'lada', 'count' => 9], ['name' => 'lada', 'count' => 2]]
Answer the question
In order to leave comments, you need to log in
As an option
$arr = [['name' => 'ford', 'count' => 2],
['name' => 'ford', 'count' => 8],
['name' => 'lada', 'count' => 9],
['name' => 'lada', 'count' => 2]];
$result = [];
foreach($arr as $car) {
if (!isset($result[$car['name']]) || $result[$car['name']] < $car['count']) {
$result[$car['name']] = $car['count'];
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question