How to perform a true false comparison of two different array values

I have two arrays:

$array_one = array(1=>6000,2=>500);
$array_two = array(1=>6500,2=>250);

      

I would like to compare values ​​with >

or <

like this:

if(6000 > 6500){
    echo "ok";
}else{ echo "not allowed";}

if(500> 250){
    echo "ok";
}else{ echo "not allowed";}

      

How can I accomplish this type of operation with a loop or something?

+3


source to share


3 answers


You access the values ​​of the array using the square bracket notation [index]

, so you can simply refer to the values ​​using their index;

if($array_one[1] > $array_two[1]) {
    echo "ok";
}
else {
    echo "not allowed";
}

      

and then you can put that in a loop like:



for($i=1;$i<=count($array_one);$i++) {
    if($array_one[$i] > $array_two[$i]) {
        echo "ok";
    }
    else {
        echo "not allowed";
    }
}

      

Hope it helps.

+4


source


Try the following:



   <?php

    foreach($array_one as $key => $value) {
        if($value > $array_two[$key]) {
            echo "OK";
        } else {
            echo "Not Allowed";
        }
    }

   ?>

      

+2


source


Try the following:

$array_one = array(1=>6000,2=>500);
$array_two = array(1=>6500,2=>250); 

foreach($array_one as $k => $v)
{
    if($v > $array_two[$k]){
    echo "ok";
    }else{ echo "not allowed";}
}

      

+1


source







All Articles