Arrays in PHP is a type of data structure that allows us to store multiple elements of similar data type under a single variable thereby saving us the effort of creating a different variable for every data. The arrays are helpful to create a list of elements of similar types, which can be accessed using their index or key. Suppose we want to store five names and print them accordingly. This can be easily done by the use of five different string variables. But if instead of five, the number rises to a hundred, then it would be really difficult for the user or developer to create so many different variables. Here array comes into play and helps us to store every element within a single variable and also allows easy access using an index or a key. An array is created using an array() function in PHP.
There are basically three types of arrays in PHP:
- Indexed or Numeric Arrays: An array with a numeric index where values are stored linearly.
- Associative Arrays: An array with a string index where instead of linear storage, each value can be assigned a specific key.
- Multidimensional Arrays: An array which contains single or multiple array within it and can be accessed via multiple indices.
Indexed or Numeric Arrays
These type of arrays can be used to store any type of elements, but an index is always a number. By default, the index starts at zero. These arrays can be created in two different ways as shown in the following example:
Accessing the 1st array elements directly: Ram Zack Raghav Accessing the 2nd array elements directly: RAM ZACK RAGHAV
Traversing: We can traverse an indexed array using loops in PHP. We can loop through the indexed array in two ways. First by using for loop and secondly by using foreach. You can refer to PHP | Loops for the syntax and basic use.
Looping using foreach: Zack Anthony Ram Salim Raghav The number of elements is 5 Looping using for: ZACK ANTHONY RAM SALIM RAGHAV
These types of arrays are similar to the indexed arrays but instead of linear storage, every value can be assigned with a user-defined key of string type.
Accessing the elements directly: zara sara any Rani Ravina
Traversing Associative Arrays: We can traverse associative arrays in a similar way did in numeric arrays using loops. We can loop through the associative array in two ways. First by using for loop and secondly by using foreach. You can refer to PHP | Loops for the syntax and basic use.
Looping using foreach: Husband is Zack and Wife is Zara Husband is Anthony and Wife is Any Husband is Ram and Wife is Rani Husband is Salim and Wife is Sara Husband is Raghav and Wife is Ravina Looping using for: zack zara anthony any ram rani salim sara raghav ravina
Multi-dimensional arrays are such arrays that store another array at each index instead of a single element. In other words, we can define multi-dimensional arrays as an array of arrays. As the name suggests, every element in this array can be an array and they can also hold other sub-arrays within. Arrays or sub-arrays in multidimensional arrays can be accessed using multiple dimensions.
Dave Punk email-id is: email@example.com John Flinch mobile number is: 9875147536
Traversing Multidimensional Arrays: We can traverse through the multidimensional array using for and foreach loop in a nested way. That is, one for loop for the outer array and one for loop for the inner array.
Dave Punk mob : 2584369721 email : firstname.lastname@example.org John Flinch mob : 9875147536 email : email@example.com
This article is contributed by Chinmoy Lenka. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.