PHP Array

Arrays in php

PHP Array is an ordered map (contains value on the basis of key). It is used to hold multiple values of similar type in a single variable.

An array is a data structure that stores one or more similar type of values in a single value. For example if you want to store 100 numbers then instead of defining 100 variables its easy to define an array of 100 length.

Advantage of PHP Array

  • Less Code
  • Easy to traverse
  • Sorting

PHP Array Types

There are 3 types of array in P.H.P.

  • Indexed Array
  • Associative Array
  • Multidimensional Array

Indexed Array

PHP index is represented by number which starts from 0. We can store number, string and object in the array. All array elements are assigned to an index number by default.

There are two ways to define indexed array:

1st way:


2nd way:





Example :



echo "Season are: $season[0], $season[1], $season[2] and $season[3]";


Associative Array

We can associate name with each array elements in P.H.P using => symbol.

There are two ways to define associative array:

1st way:


2nd way:




Example :



echo "Sonoo salary: ".$salary["Sonoo"]."<br/>";

echo "John salary: ".$salary["John"]."<br/>";

echo "Kartik salary: ".$salary["Kartik"]."<br/>";


PHP Multidimensional Array

A multi-dimensional array each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. Values in the multi-dimensional array are accessed using multiple index.

Example :





         $marks = array( 

            "mohammad" => array (

               "physics" => 35,

               "maths" => 30,   

               "chemistry" => 39



            "qadir" => array (

               "physics" => 30,

               "maths" => 32,

               "chemistry" => 29



            "zara" => array (

               "physics" => 31,

               "maths" => 22,

Spread the love

Leave a Comment

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