php check array exists code example

Example 1: php array index exists

// Here's our fruity array
$fruits = ['apple', 'pear', 'banana'];

// Use it in an `if` statement
if (array_key_exists("banana", $fruits)) {
 // Do stuff because `banana` exists
}

// Store it for later use
$exists = array_key_exists("peach", $fruits);

// Return it directly
return array_key_exists("pineapple", $fruits);

Example 2: php array has value

$myArr = [38, 18, 10, 7, "15"];

echo in_array(10, $myArr); // TRUE
echo in_array(19, $myArr); // TRUE

// Without strict check
echo in_array("18", $myArr); // TRUE
// With strict check
echo in_array("18", $myArr, true); // FALSE

Example 3: php array index exists

<?php
$array1=array("Orange" => 100, "Apple" => 200, "Banana" => 300, "Cherry" => 400);
if (array_key_exists("Banana",$array1))
{
echo "Array Key exists...";
}
else
{
echo "Array Key does not exist...";
}
?>

Tags:

Php Example