Arrays in PHP

Arrays in PHP

In other programming languages arrays are used to store multiple values of same type but PHP has one more advantage since it’s not a type language so arrays can save values of different data types at same place.

Declaration of array in PHP:

In PHP array() function is used to create an array:

 

Types of Arrays:

There are two types of arrays in PHP

  • Numeric arrays
  • Associative arrays

Numeric Arrays or Indexed Arrays:

As the name, these arrays are same as we have in other programming languages they have simple numeric indexing starts from zero. We don’t define the index of arrays.

indexed arrays in PHP
Indexed arrays in PHP

Loop on numeric arrays:

To loop numeric or indexed array we have to use simple for or foreach loop.

 

Find Length of array in PHP:

To find the length of array in PHP count () function is used. This function returns the length of the array which we pass in it as argument.

 

Associative arrays in PHP:

This type of array uses key to assign value on index. Instead of auto numeric indexing you should define every index.

Associative arrays in PHP
Associative arrays in PHP

Loop on associative array:

Multidimensional Array:

In PHP same like JavaScript multidimensional or 2D array is array with in another array.