Skip to content

Instantly share code, notes, and snippets.

@foxted
Created December 10, 2014 02:04
Show Gist options
  • Save foxted/d703e8271b1d78e34b96 to your computer and use it in GitHub Desktop.
Save foxted/d703e8271b1d78e34b96 to your computer and use it in GitHub Desktop.
Max in array of objects
<?php
class Temperature{
private $temperature;
public function __construct($value){
$this->temperature = $value;
}
public function getTemperature()
{
return $this->temperature;
}
}
$objects = [];
foreach(range(1,5) as $index)
{
array_push($objects, new Temperature(rand(1, 100)));
}
//var_dump($objects);
$max = max(array_reduce($objects, function($v, $w){
return max($v, $w->getTemperature());
}), -9999999);
//var_dump($max);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment