PHP array Function -array()

PHP array  Function is used to create new arrays.

For  example:

Below example create array called animals and assign its 3 elements.All the elements are printed using  echo function.



echo "I like " . $animals[0] . ", " . $animals[1] . " and " . $animals[2] . ".";


As  i stated earlier in my post about PHP Arrays,there are three types of arrays.Namely

  • Indexed arrays – these are arrays with only one key or one index
  • Associative arrays – These are arrays that contain named
  • Multidimensional arrays – These are arrays that have more than one array inside them.

PHP array Function

Syntax used to create indexed arrays:


Syntax used to create associative arrays:



1.How to Create associative array called Marks;



echo "Peter is " . $marks['Peter'] . " years old.";


Example 2

How to loop through a indexed array,get the values and print them.


$animals=array("Goat ","Cow","Donkey");





echo $animals[$x];

echo "<br>";



Example 3

Below example shows how one can loop through values contained in associative array,get their values and print them.




foreach($marks as $y=>$y_value)


echo "Key=" . $y . ", Values=" . $y_value;

echo "<br>";



Example 4

How to create a multidimensional array


// A two-dimensional array:








See Complete  Array Functions :PHP Array Functions