Created
December 10, 2014 02:04
-
-
Save foxted/d703e8271b1d78e34b96 to your computer and use it in GitHub Desktop.
Max in array of objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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