Leandro Cintrao
Leandro Cintrao

Reputation: 69

Comparing arrays and execute code depending on result

I can't seem to find a way to this in PHP using arrays.

$id_qty1 = array(1 => 20, 2 => 30, 3 => 40); 

$id_qty2 = array(1 => 30, 2 => 20, 3 => 50); 

I would like to check one array against the other and:

if same key and bigger value do one thing

if same key and smaller value do another thing

Upvotes: 1

Views: 36

Answers (1)

Brewal
Brewal

Reputation: 8189

Something like this would do it :

foreach ($id_qty1 as $key => $value) {
    if (isset($id_qty2[$key])) {
        if ($id_qty2[$key] > $value) {
            // do one thing
        } else {
            // do another thing 
        }
    }
}

Upvotes: 2

Related Questions