mardi 29 janvier 2019

Why won't the value I am searching in an Array return true even though the matching value exists?

I have been trying to figure this out for the last 3 hours. I generate a random number, 0 - 36. I also generate an array with the numbers 0-36 in steps of 2 (odd numbers only). I do a var_dump on both the random number and the array and I can see the matching value in the array, however, my if statement won't return true.

I also tried in_array and that did not work. I tried array_map, no luck... I have googled endlessly and tried every thing I can think of. What gives?

$this->number = rand(0, 36); 
$this->colorBlack = array(range(1, 36, 2));

foreach ($this->colorBlack as $this->color){

            var_dump($this->color);
            var_dump($this->number);

        if ($this->color == $this->number){
            echo 'yes';
            var_dump($this->colorBlack);
        }
    }

I would expect the above code to return true when the random number generated matches a value in the array, however that is not the case.

the Var dump looks like this:

array(18) { [0]=> int(1) [1]=> int(3) [2]=> int(5) [3]=> int(7) [4]=> int(9) [5]=> int(11) [6]=> int(13) [7]=> int(15) [8]=> int(17) [9]=> int(19) [10]=> int(21) [11]=> int(23) [12]=> int(25) [13]=> int(27) [14]=> int(29) [15]=> int(31) [16]=> int(33) [17]=> int(35) } int(26)

Aucun commentaire:

Enregistrer un commentaire