Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP equivalent to max function in lodash/underscore

Tags:

php

Is there a PHP v5.5 equilavent to max for lodash/underscore?

Basically I would like to be able to do something similar to this:

$characters = [
  { 'name': 'barney', 'age': 36 },
  { 'name': 'fred',   'age': 40 }
];

max($characters, 'age');
// → { 'name': 'fred', 'age': 40 };
like image 843
Enrique Moreno Tent Avatar asked Mar 25 '26 05:03

Enrique Moreno Tent


1 Answers

There is a port of Underscore called Underscore.php which provides the max function (as well as many other Underscore functions).

From its documentation:

$stooges = array(
  array('name'=>'moe', 'age'=>40),
  array('name'=>'larry', 'age'=>50),
  array('name'=>'curly', 'age'=>60)
);
__::max($stooges, function($stooge) { return $stooge['age']; });
// array('name'=>'curly', 'age'=>60)
like image 78
The Doge Prince Avatar answered Mar 26 '26 20:03

The Doge Prince