xxxxxxxxxx
// Sample object
$myObject = (object) [
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3'
];
// Key to check
$key = 'key2';
// Check if key exists in the object
if(array_key_exists($key, get_object_vars($myObject))){
echo 'Key exists in the object.';
} else {
echo 'Key does not exist in the object.';
}
xxxxxxxxxx
$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_search("red",$a);
//output = a;
xxxxxxxxxx
<?php
$exampleArray = array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3'
);
$desiredKey = 'key2';
if (array_key_exists($desiredKey, $exampleArray)) {
echo "The key '$desiredKey' exists in the array.";
} else {
echo "The key '$desiredKey' does not exist in the array.";
}
?>
xxxxxxxxxx
// Sample array
$array = array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3'
);
// Key to check
$key = 'key2';
// Check if key exists
if (array_key_exists($key, $array)) {
echo "The key '$key' exists in the array.";
} else {
echo "The key '$key' does not exist in the array.";
}