Sounds like a good job for the functional reduce approach. You can do this in PHP with the array_reduce function:
You pass in an array, a callback and a starting value and the function will call the callback with the current value and the next item from the array and store the result.
php> $array = [ 6, 2, 8, 4 ];
array (
0 => 6,
1 => 2,
2 => 8,
3 => 4,
)
php> array_reduce($array, 'min', reset($array));
int(2)
php> array_reduce($array, 'max', reset($array));
int(8)
In this example I used min and max respectively as the callback and the first array item as the starting value.
In order to use this properly on your array you can pass in a custom callback using an anonymous function:
function ($a, $b) {
return max($a->ordering, $b->ordering);
}
$result_1look like? (Show us whatvar_dump($result_1)outputs.)Warning: max() [function.max]: When only one parameter is given, it must be an array inmax()andmin()on that array? Clearly if you want to analyze a set of values like this, you need to store them in a structure that allows you to do so rather then print every single case where your criteria is met to screen.$result_1.