How to check an array is associative or sequential in PHP? Last Updated : 24 Sep, 2018 Comments Improve Suggest changes Like Article Like Report In PHP there is no need to write the variable type before the variable because it is loosely-typed. It takes datatype from user defined values that are stored in it. Arrays in PHP is a type of data structure that allows to store multiple elements of similar data type under a single variable thereby saving the effort of creating a different variable for every data. There are basically three types of arrays in PHP: Sequential (Indexed) arrays Associative arrays Multidimensional arrays Sequential Arrays: Those arrays which have numerical indexes in an ordered sequential manner (starting from 0 and ending with n-1) are called Sequential or Indexed arrays. In PHP, by default array Indexed array. php <?php // Example of sequential array $arr = array("January", "February", "March"); // 1st element echo $arr[0] . "\n"; // 2nd element echo $arr[1] . "\n"; // 3rd element echo $arr[2] . "\n"; ?> Output: January February March Associative arrays: The arrays which are having string type keys instead of indexes or which exist in ('key', 'value')pairs are called associative arrays. php <?php // Example of associative array $arr1= array("Month1" => "January", "Month2" => "February", "Month3" => "March" ); echo $arr1["Month1"] . "\n"; echo $arr1["Month2"] . "\n"; echo $arr1["Month3"] . "\n"; ?> Output: January February March How to check if PHP array is associative or sequential? There is no inbuilt method in PHP to know the type of array. If the sequential array contains n elements then their index lies between 0 to (n-1). So find the array key value and check if it exist in 0 to (n-1) then it is sequential otherwise associative array. php <?php // Example to check Sequential array // Associative array $arr1= array("Month1" => "January", "Month2" => "February", "Month3" => "March" ); // Checking for sequential keys of array arr if(array_keys($arr1) !== range(0, count($arr1) - 1)) echo "Array is associative"; else echo "Array is sequential \n"; ?> Output: Array is associative Note: === It returns True if $x and $y are equal and same type. !== It returns True if $x and $y are not equal or not same type. == It returns True if $x and $y are equal. != It returns True if $x not equal to $y. Comment More infoAdvertise with us Next Article How to check an array is associative or sequential in PHP? P piyush25pv Follow Improve Article Tags : Web Technologies PHP PHP Programs Similar Reads How to Get All Keys of an Associative Array in PHP? Given an Associative array, the task is to get all the keys of the associative array in PHP. There are two approaches to get all keys from the associative array, these are - using array_keys(), and foreach loop. In this article, we will explore these approaches with examples.Table of ContentApproach 2 min read How to Check if a Key Exists in an Associative Array in PHP? Given an Associative array, the task is to check whether a key exists in the associative array or not. There are two methods to check if a key exists in an associative array, these are - using array_key_exists(), and isset() functions. Let's explore each method with detailed explanations and code ex 3 min read How to Check if a Value Exists in an Associative Array in PHP? Given an Associative array, the task is to check whether a value exists in the associative array or not. There are two methods to check if a value exists in an associative array, these are - using in_array(), and array_search() functions. Let's explore each method with detailed explanations and code 2 min read How to loop through an associative array and get the key in PHP? Associative arrays are used to store key-value pairs. For example, to store the marks of the different subject of a student in an array, a numerically indexed array would not be the best choice. Instead, we could use the respective subjectâs names as the keys in our associative array, and the value 3 min read How to access an associative array by integer index in PHP? There are two types of arrays in PHP, indexed and associative arrays. In case of indexed array strict numeric indexing is followed but in case of associative array there are keys corresponding to each element. The elements of an associative array can only be accessed by the corresponding keys. As th 3 min read Like