How to use key_exists() Function – Alias of array_key_exists() Function In PHP
The key_exists() function is an alias of array_key_exists() and works similarly.
Example: This example shows the use of the above-explained approach.
<?php
function checkValExist($arr, $index) {
return key_exists($index, $arr);
}
// Driver code
$arr = [1, 2, 3, 4, 5];
$index = 3;
if (checkValExist($arr, $index)) {
echo "Value at index $index exists and is: "
. $arr[$index];
} else {
echo "No value exists at index $index.";
}
?>
Output
Value at index 3 exists and is: 4
Explanation:
- key_exists($index, $array) checks if the specified index exists in the array.
- If the index exists, the function returns true; otherwise, it returns false.
How to Check a Value Exist at Certain Index in an Array in PHP?
Given an array and index, the task is to check whether a value exists or not at a certain index in an array in PHP.
Below are the approaches to check if a value exists at a certain index or not in an array in PHP:
Table of Content
- Using isset() Function
- Using array_key_exists() Function
- Using key_exists() Function – Alias of array_key_exists() Function