Detect if I change the value of a key in the next round of the foreach?

0

I need to find out if the value of a key changes on the next round

Ex:

if($manzanas[$key]['cantidad'] == $manzanas[$key + 1]['cantidad']){
   echo 'La cantidad de manzanas es la misma';
} else {
   echo 'La cantidad de manzanas cambio';
}

That's not how it works and I can not find the right way to do it.

    
asked by Melina Onoriaga 12.01.2018 в 17:58
source

1 answer

-1

I guess you throw a mistake when trying to find 'cantidad' within a $key non-existent, as you are doing a $key + 1 , will reach a point where the next does not exist and therefore you print an error. To fix it you should add one more check before asking if the following is the same:

if (isset($manzanas[$key + 1]) && $manzanas[$key]['cantidad'] == $manzanas[$key + 1]['cantidad']) {
   echo 'La cantidad de manzanas es la misma';
} else {
   echo 'La cantidad de manzanas cambio';
}
    
answered by 12.01.2018 в 18:24