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
The isset() function determines if a variable is set and is not null. It can be used to check if an index exists in an array.
Example: This example shows the use of the above-explained approach.
<?php
function checkValExist($arr, $index) {
return isset($arr[$index]);
}
// 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:
- isset($array[$index]) checks if the index exists in the array and the value is not null.
- If the index exists, the function returns true; otherwise, it returns false.
Using array_key_exists() Function
The array_key_exists() function checks if the specified key or index exists in the array.
Example: This example shows the use of the above-explained approach.
<?php
function checkValExist($arr, $index) {
return array_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:
- array_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.
Using key_exists() Function - Alias of array_key_exists() Function
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.
Using ArrayObject Class
The ArrayObject class allows objects to work as arrays. It has a method called offsetExists which can be used to check if an index exists.
Example: This example shows the use of the above-explained approach.
<?php
function checkValExist($arr, $index) {
$arrayObject = new ArrayObject($arr);
return $arrayObject->offsetExists($index);
}
// 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