Dawny33
0
Q:

php key 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);
3
<?php 
// PHP function to illustrate the use 
// of array_key_exists() 
function Exists($index, $array) 
{ 
    if (array_key_exists($index, $array)){ 
        echo "Found the Key"; 
    } 
    else{ 
        echo "Key not Found"; 
    } 
} 
  
$array = array("ram"=>25, "krishna"=>10,  
                "aakash"=>20, "gaurav"); 
$index = "aakash"; 
print_r(Exists($index, $array)); 
?> 
3

<?php
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
    echo "The 'first' element is in the array";
}
?>

1

New to Communities?

Join the community