How to check an array is associative or sequential in PHP?

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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";
?>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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";
?>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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";
?>

chevron_right


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.


My Personal Notes arrow_drop_up

Im a final year MCA student at Panjab University, Chandigarh, one of the most prestigious university of India I am skilled in various aspects related to Web Development and AI I have worked as a freelancer at upwork and thus have knowledge on various aspects related to NLP, image processing and web

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.