dimanche 24 mars 2019

PHP: If condition not working inside foreach loop

I want to show data using a PHP foreach loop that has an If stattement inside. The If statement is accepting some values as TRUE when it should be accepting ALL the values as true.

Printing $phone_array:

[0] => Array
    (
        [phone_number] => (011) 5290-0314
        [type] =>  phone
    )

[1] => Array
    (
        [phone_number] => (011) 4711-6948
        [type] =>  phone
    )

[2] => Array
    (
        [phone_number] => (011) 4794-9490
        [type] => phone
    )

The if statement inside the foreach loop:

foreach($phone_array as $value){
    if(isset($value['type']) && $value['type'] == 'phone'){
        echo "Its a phone<br>";
    }
    elseif(isset($value['type']) && $value['type'] == 'mobile'){
        echo "Its a mobile phone<br>";
    }
}

This code shows "Its a phone" only once when it should be showing it 3 times.

Aucun commentaire:

Enregistrer un commentaire