How to get max value in foreach php?

I'm fairly new to php and i have problem with loops. I have a foreach loop,

foreach [$contents as $g => $f]
{
  p[$f];
}

which gives some arrays, depending on how many contents i have. currently i have 2,

Array
[
    [quantity] => 1
    [discount] => 1
    [discount_id] => 0
    [id] => 1506
    [cat_id] => 160
    [price] => 89
    [title] => კაბა
]

Array
[
    [quantity] => 1
    [discount] => 1
    [discount_id] => 0
    [id] => 1561
    [cat_id] => 160
    [price] => 79
    [title] => ზედა
]

my goal is to save the array which has the max price in it as a different variable. I'm kinda stuck on how to do that, i managed to find the max price with the max[] function like so

foreach [$contents as $g => $f]
{

    $priceprod[] = $f['price'];
    $maxprice = max[$priceprod];
   p[$maxprice];
}

but i still dont get how i'm supposed to find out in which array is the max price. any suggestions would be appreciated

asked May 17, 2014 at 15:09

GuramGuram

1471 silver badge9 bronze badges

You should store the keys as well so that you can look it up after the loop:

$priceprod = array[];

foreach [$contents as $g => $f]
{
  // use the key $g in the $priceprod array
  $priceprod[$g] = $f['price'];
}

// get the highest price
$maxprice = max[$priceprod];

// find the key of the product with the highest price
$product_key = array_search[$maxprice, $priceprod];

$product_with_highest_price = $contents[$product_key];

Note that the results will be unreliable if there are multiple products with the same price.

answered May 17, 2014 at 15:21

jeroenjeroen

90.3k21 gold badges113 silver badges131 bronze badges

1

Check max function for the array in outside the loop.

foreach [$contents as $g => $f]
{

    $priceprod[] = $f['price'];

}
$maxprice = max[$priceprod];
p[$maxprice];

answered May 17, 2014 at 15:12

Balaji KandasamyBalaji Kandasamy

4,17710 gold badges39 silver badges58 bronze badges

2

Here you got single loop solution handling multiple items with same max price.

$maxPrice = - INF;
$keys = [];
foreach[$contents as $k=>$v]{
    if[$v['price']>$maxPrice]{
        $maxPrice = $v['price'];
        $keys = [$k];
    }else if[$v['price']==$maxPrice]{
        $keys[] = $k;
    }
}

answered May 18, 2014 at 8:08

mlekomleko

10.8k6 gold badges50 silver badges71 bronze badges

Now a new problem.

I get the values from the code posted above but I just get the word Array when I try to use it with max[]

$arr = array[];
foreach[$array as $key => $add]{
$image_height = round[$_SESSION['imgheight'.$key]];
if[$add == "Yes"]
{
  $arr[] = $image_height;
}
}

rsort[$arr];

print_r[$arr];

That gives me

Array [ [0] => 379 [1] => 8 ]

for the print_r[$arr];

But when I try to use the MAX[] function with it

I use  max[array[$arr]];

And it just prints Array instead of the highest value??

I then tried putting it into a string

$works = implode[',', $arr];

echo $works;
$hgt = max[$works];

echo works gives me a comma sep. string of the values

But, I get Wrong parameter count for max when I use the variable.

Is there a different way to get the highest value?

How to get maximum value from array in PHP using Library function, foeach Loop, for loop

PHP get max value in array using Library function

PHP get max value in array using foreach

PHP get max value in array using for loop

How to get array max value in PHP?

The max[] function of PHP is used to find the numerically maximum value in an array or the numerically maximum value of several specified values. The max[] function can take an array or several numbers as an argument and return the numerically maximum value among the passed parameters.

How to get min and max value from array in PHP?

$min = min[$numbers]; $max = max[$numbers]; Option 2. [Only if you don't have PHP 5.5 or better] The same as option 1, but to pluck the values, use array_map : $numbers = array_map[function[$details] { return $details['Weight']; }, $array];

How do you find the max value in an array?

To get the index of the max value in an array:.
Get the max value in the array, using the Math. max[] method..
Call the indexOf[] method on the array, passing it the max value..
The indexOf method returns the index of the first occurrence of the value in the array or -1 if the value is not found..

Chủ Đề