php - foreach max array value? -
i'm new php , have problem loops. have foreach loop,
foreach ($contents $g => $f) { p($f); }
which gives arrays, depending on how many contents have. 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 save array has max price in different variable. i'm kinda stuck on how that, managed find max price max()
function so
foreach ($contents $g => $f) { $priceprod[] = $f['price']; $maxprice = max($priceprod); p($maxprice); }
but still dont how i'm supposed find out in array max price. suggestions appreciated
you should store keys can after loop:
$priceprod = array(); foreach ($contents $g => $f) { // use key $g in $priceprod array $priceprod[$g] = $f['price']; } // highest price $maxprice = max($priceprod); // find key of product highest price $product_key = array_search($maxprice, $priceprod); $product_with_highest_price = $contents[$product_key];
note results unreliable if there multiple products same price.
Comments
Post a Comment