PHP

PHP Arrays

PHP Arrays

It is a type of data structure wherein a single value one or more similar types of values are stored. Let’s understand it with the help of an example – suppose you have 20 numbers so for this you will need to define those 20 numbers instead of doing it you can put them in an array of length 20 as they are of a similar type (i.e.- numbers). There are different types of the array and each array is accessed by an array index. 

  • Numeric array – In this the values are accessed in a linear fashion having an array index as a numeric index. The numeric index starts with zero and can store numbers, strings, and any object.
  • Associative array –  this array is similar to a numeric array only in terms of functionality but the index terms used are different.it has a string as an array index which stores array values in association with key values rather than sticking to the linear index order. 

For example – suppose you want to store the salaries of the employees in such a case the associative array is used where we can use the names of the employees as keys and the respective salary will be its value.

  • Do not print the associative array in double-quotes as it would not return any value.
  • Multidimensional array- Multiple indices are used to access this array which contains one or more arrays and values.