How to Check a Value Exist at Certain Index in an Array in PHP?
Last Updated :
31 Jul, 2024
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:
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
<?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.";
}
?>
OutputValue 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
<?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.";
}
?>
OutputValue 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
<?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.";
}
?>
OutputValue 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
<?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.";
}
?>
OutputValue at index 3 exists and is: 4
Similar Reads
How to Check a Value Exist at Certain Array Index in JavaScript ? Determining if a value exists at a specific index in an array is a common task in JavaScript. This article explores various methods to achieve this, providing clear examples and explanations to enhance your coding efficiency.Below are the different ways to check if a value exists at a specific index
5 min read
How to check an element is exists in array or not in PHP ? An array may contain elements belonging to different data types, integer, character, or logical type. The values can then be inspected in the array using various in-built methods : Approach 1 (Using in_array() method): The array() method can be used to declare an array. The in_array() method in PHP
2 min read
How to check if an Array contains a value or not? There are many ways for checking whether the array contains any specific value or not, one of them is: Examples: Input: arr[] = {10, 30, 15, 17, 39, 13}, key = 17Output: True Input: arr[] = {3, 2, 1, 7, 10, 13}, key = 20Output: False Approach: Using in-built functions: In C language there is no in-b
3 min read
How to find the index of an element in an array using PHP ? In this article, we will discuss how to find the index of an element in an array in PHP. Array indexing starts from 0 to n-1. Here we have some common approachesTable of ContentUsing array_search() FunctionUsing array_flip()Using a custom functionUsing a foreach LoopUsing array_keys FunctionUsing ar
6 min read
How to get total number of elements used in array in PHP ? In this article, we will discuss how to get total number of elements in PHP from an array. We can get total number of elements in an array by using count() and sizeof() functions. Using count() Function: The count() function is used to get the total number of elements in an array. Syntax: count(arra
2 min read