
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if an Array is Associative or Sequential in PHP
What is Associative Array?
An associative array is a type of array in which each element is associated with a specific key, rather than being assigned a numeric index like in sequential arrays. In other words, instead of accessing array elements by their position, you access them by using their corresponding keys.
In an associative array, the keys can be either strings or integers. Each key is unique within the array, meaning that no two elements can have the same key. The values in an associative array can be of any data type, such as strings, numbers, booleans, objects, or even other arrays.
Example
<?php $person = array( 'name' => 'John', 'age' => 30, 'city' => 'New York' ); echo $person['name']; echo $person['age']; echo $person['city']; ?>
Output
John30New York
What is Sequential Array?
A sequential array, also known as an indexed array, is a type of array in which the elements are assigned numeric indices that represent their position in the array. The indices start from 0 for the first element and increment by 1 for each subsequent element.
In a sequential array, the keys of the elements are automatically assigned by PHP, based on the order in which the elements are added to the array. These keys are integers that represent the position or index of each element.
Example
<?php // Example of sequential array $arr = array(10,20,30,40); // 1st element echo $arr[0] . "
"; // 2nd element echo $arr[1] . "
"; // 3rd element echo $arr[2] . "
"; // 4th element echo $arr[3] . "
"; ?>
Output
10 20 30 40
How to check an Array is Associative or Sequential in PHP
To check if an array is associative or sequential in PHP, you can use the array_keys() function and compare the resulting array of keys with the original array.
Array_keys() function
The array_keys() function in PHP is used to retrieve all the keys or a subset of keys from an array. It returns a new array containing the keys of the input array.
Syntax
The general syntax of the array_keys() function is:
array_keys(array $array, mixed $search_value = null, bool $strict = false): array
Parameters
$array: The input array from which you want to extract the keys.
$search_value (optional): If provided, array_keys() will only return the keys for the given value.
$strict (optional): Determines whether the comparison should be strict (true) or loose (false). By default, it is set to false, which means loose comparison is used.
Example
<?php $array = ['apple', 'banana', 'orange']; $keys = array_keys($array); if ($keys !== range(0, count($array) - 1)) { // Associative array echo 'Associative array'; } else { // sequential array echo 'Sequential array'; } ?>
Output
Sequential array
In this example, we have an array $array with elements 'apple', 'banana', and 'orange'. We use the array_keys() function to retrieve the keys of the array, and then we compare those keys with the range of numeric indices from 0 to count($array) - 1 using the !== operator. If the keys of the array ($keys) are not equal to the expected sequential indices, it indicates that the array is associative. In this case, the output will be 'associative array'. If the keys of the array match the expected sequential indices, it indicates that the array is sequential. In this case, the output will be 'sequential array'.
Conclusion
In PHP, you can check whether an array is associative or sequential by examining its keys. If the keys are non-numeric or not in a sequential numeric order starting from 0, the array is considered associative. Otherwise, if the keys are sequential integers starting from 0, the array is sequential. To perform this check, you can use the array_keys() function to retrieve the array keys and compare them against expected patterns.