Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check value against each value in array

Tags:

arrays

php

I would like to check if

$price_a = 30;

is greater than all the values in

$all_prices = [
    1 => 12,
    2 => 24,
    3 => 32,
    4 => 44
];

I guess I'd have to loop through the array and do the check for each iteration but I'm not sure how that loop should look like. Or is there a simpler solution?

like image 447
Tim Kohlen Avatar asked Jul 12 '26 06:07

Tim Kohlen


1 Answers

You can check it by using max function of php :

<?php 
    if($price_a > max($all_prices)){
        echo "Greater than all the values";
    }
    else{
        echo "Smaller than ".max($all_prices);
    }
?>
like image 184
vikalp Avatar answered Jul 13 '26 19:07

vikalp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!