Menu

Add Topic

programming

waiting answer July 28, 2021

How to check if an array value exists

$selected_colours = array('blue','yellow','white');

How can I check if $selected_colours has the value of 'green'?

Answers
June 10, 2021

The in_array() is a PHP inbuilt function that is used to check whether a given value exists in an array or not. It returns either TRUE or FALSE. The function returns TRUE if the given value exists in the array. else return false.

0 0
June 10, 2021

You can check this using in_array() function in PHP

    $selected_colours = array('blue','yellow','white');

    if( in_array( "green" ,$selected_colours ) )
    {
        echo "green colour exists";
    }else{
        echo "green colour not exists";
    }
0 0
June 10, 2021

in_array ( $value_to_check, $array, $mode );

$mode is an optional parameter. If you need to do an strict check, you pass a $mode = TRUE. By default, $mode = FALSE and no need to check.

    // With strict check
    echo in_array("green", $selected_colours, true); 

    // Without strict check    
    echo in_array("green", $selected_colours); 

0 0

Please Login to Post the answer

Leave an Answer