<?php
function max_by($collection, $property) {
$maxElement = $collection[0];
for ($i=0; $i < sizeof($collection); $i++) {
if ($collection[$i]->$property > $maxElement->$property) {
$maxElement = $collection[$i];
}
}
return $maxElement;
}
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:
48.32 ms | 402 KiB | 5 Q