PHP : Check if an item is in an array

In this tutorial learn how to Check if an item is in an array or not in array using in_array() method

function contains($array, $value)
    {
        return in_array($value, $array, true);
    }



Tutorial


<?php

$array = Array
(
            'user_id' => 10,
            'id' => 2,
           'user' => 'Nakiya',

);

 /**
     * Check if an item is in an array.
     */
function contains($array, $value)
    {
        return in_array($value, $array, true);
    }

    
$result_1 = contains($array,'Nakiya');

echo "item available =>";

if($result_1){
    echo "success";
}else{
    echo "fail";
    
}
echo " item not available =>";
$result_2 = contains($array,'Nakiya10');
if($result_2){
    echo "success <br>";
}else{
    echo "fail";
    
}

OUTPUT DEMO:

item available =>success item not available =>fail


No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...