<?php
function max_by($collection, $property) {
$plucked = array_map(function ($item) use ($property) {
return $item->$property;
}, $collection);
return max($plucked);
}
class A {
function __construct($u,$v) {
$this->u = $u;
$this->v = $v;
}
}
$array = [
new A(1,5),
new A(2,2),
new A(3,1)
];
$a = max_by($array, 'u');
$b = max_by($array, 'v');
var_dump($a, $b);
preferences:
39.28 ms | 402 KiB | 5 Q