What are the types of arrays in PHP ?

  • In PHP an array 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.
  • Using their index or key arrays are helpful to create a list of elements of similar types, which can be accessed.
  • Suppose accordingly we want to store five names and print them and it can be easily done by the use of five different string variables.
  • Instead of five string variables, the number rises to a hundred, then it would be really difficult for the developer or user to create so many different variables.
  • Also using an index or a key array comes into play and helps us to store every element within a single variable and also allows easy access.
  • In PHP an array is created using array() function and basically there three types, they are
    1. Indexed array
    2. Associative array
    3. Multidimensional array

Indexed array

  • Indexed array is represented by number which starts from 0 and we can store integer, object and string in the PHP array.
  • By default, all PHP array elements are assigned to an index number.

Sample Code

[pastacode lang=”php” manual=”%3C%3Fphp%20%20%0A%24season%3Darray(%22January%22%2C%22February%22%2C%22March%22%2C%22April%22)%3B%20%20%0Aecho%20%22Month%20are%3A%20%24month%5B0%5D%2C%20%24month%5B1%5D%2C%20%24month%5B2%5D%20and%20%24month%5B3%5D%22%3B%20%20%0A%3F%3E%20%20%0A” message=”” highlight=”” provider=”manual”/]

Output

Associative Array

  • Associative 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.
  • In PHP we can associate name with each array elements by using => symbol.

Sample Code

[pastacode lang=”php” manual=”%3C%3Fphp%20%20%20%20%0A%24salary%5B%22Venkat%22%5D%3D%22350000%22%3B%20%20%20%20%0A%24salary%5B%22Kishore%22%5D%3D%22450000%22%3B%20%20%20%20%0A%24salary%5B%22Nizar%22%5D%3D%22200000%22%3B%20%20%20%20%0Aecho%20%22Venkat%20salary%3A%20%22.%24salary%5B%22Venkat%22%5D.%22%3Cbr%2F%3E%22%3B%20%20%0Aecho%20%22Kishore%20salary%3A%20%22.%24salary%5B%22Kishore%22%5D.%22%3Cbr%2F%3E%22%3B%20%20%0Aecho%20%22Nizar%20salary%3A%20%22.%24salary%5B%22Nizar%22%5D.%22%3Cbr%2F%3E%22%3B%20%20%0A%3F%3E%20%20%20%20%0A” message=”” highlight=”” provider=”manual”/]

Output

Multidimensional Array

  • PHP multidimensional array is otherwise known as array of arrays and allows you to store tabular data in an array.
  • In the form of matrix PHP multidimensional array can be represented which is represented by row * column.

Sample Code

[pastacode lang=”php” manual=”%24emp%20%3D%20array%20%20%0A%20%20(%20%20%0A%20%20array(1%2C%22Venkat%22%2C400000)%2C%20%20%0A%20%20array(2%2C%22Kishore%22%2C500000)%2C%20%20%0A%20%20array(3%2C%22Nizar%22%2C300000)%20%20%0A%20%20)%3B%20%20%0A” message=”” highlight=”” provider=”manual”/]

Output

 

ID

 

Name

 

Salary

 

1

 

Venkat

 

400000

 

2

 

Kishore

 

500000

 

3

 

Nizar

 

300000

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like