-2

I have an array like so:

Array
(
    [0] => Array
        (
            [rb_priceRangeId] => 3
            [rb_priceRangeMin] => 300000
            [rb_priceRangeMax] => 399999
        )

    [1] => Array
        (
            [rb_priceRangeId] => 5
            [rb_priceRangeMin] => 400000
            [rb_priceRangeMax] => 499999
        )

)

How would I get the lowest value out rb_priceRangeMin into a variable and get hightest value out of rb_priceRangeMax into a variable? I think I would have to use a foreach, but I have no idea what would go inside it. Any help would be highly appreciated

I have tried

foreach($array as $row => $value){

$min = $value['rb_priceRangeMin'];

if($min < $value['rb_priceRangeMin']){
     $min = $value['rb_priceRangeMin'];
}

$max = $value['rb_priceRangeMax'];

if($max < $value['rb_priceRangeMax']){
     $max = $value['rb_priceRangeMax'];
}

} 
user3723240
  • 395
  • 3
  • 11
  • 29
  • 1
    What have you tried? This site is to help you with problems, not write the code for you. – Pitchinnate Jul 18 '14 at 13:02
  • You could use [array_filter](http://php.net/manual/en/function.array-filter.php) to do that. – machineaddict Jul 18 '14 at 13:03
  • 1
    Why don't you insert all your "rb_priceRangeMin" & "rb_priceRangeMax" elements in one one array & over that try to use max() & min(). Can't say it'll be best solution but, i think it'll do the job. – Suresh Jul 18 '14 at 13:05
  • @user3723240 : Problem in your code is that you set `$min` and `$max` two times in foreach. Remove it on line 3 and 9. – fdehanne Jul 18 '14 at 13:13

2 Answers2

1
$minmax = array('min' => $array[0]['rb_priceRangeMin'], 'max' => 0);
foreach ($array as $val) {
    if ($val['rb_priceRangeMin'] < $minmax['min']) { $minmax['min'] = $val['rb_priceRangeMin']; }
    if ($val['rb_priceRangeMax'] > $minmax['max']) { $minmax['max'] = $val['rb_priceRangeMax']; }
}
t3chguy
  • 1,018
  • 7
  • 17
1
$minValue = false;
$maxValue = false;

foreach( $array as $a )
{
    if ( $minValue === false || $a['rb_priceRangeMin'] < $minValue ) $minValue = $a['rb_priceRangeMin'];
    if ( $maxValue === false || $a['rb_priceRangeMax'] > $maxValue ) $maxValue = $a['rb_priceRangeMax'];
}
fdehanne
  • 1,658
  • 1
  • 16
  • 32