PHP Arrays

php-array

An array stores multiple values/data/string in an single variable.

<?php
$name = array("Vijay","Gopi","Edwin","Munee");
echo "Members are ".$name[0].",".$name[1].",".$name[2].",".$name[3];

?> 

Output: Members are Vijay,Gopi,Edwin,Munee

Array Definition:

An array is an special variable which holds more than one / multiple variable at a time.

If you have a list of data, storing the names should be could look like this

$name1 = 'Vijay';
$name2 = 'Gopi';
$name3 = 'Edwin';
$name4 = 'Munee';

If you have more number of data, example more than 10 members in a team we can use array instead of creating 10 different variable.

Creation of Array

In PHP the array will create using array() function.

There are three types of arrays are there in PHP.

  • Indexed Array ( Array with numeric index )
  • Associative Array ( Array with named key )
  • Multidimensional Array ( Array containing multiple arrays with in it )

Indexed Array

An array with numeric index is called Indexed array

<?php
$a = array("IND","US","AUS");
echo $a[0]."<br />";
echo $a[1]."<br />";
echo $a[2]."<br />";
?>

Output:
IND
US
AUS

Associative Array

An array with named key is call as Associative array

<?php
$a = array("India"=>"IND","United States"=>"US","Australia"=>"AUS");
echo $a['India']."<br />";
echo $a['United States']."<br />";
echo $a['Australia']."<br />";
?>

Output:
IND
US
AUS

Multidimensional Array

An array containing multiple array with in it called as Multidimensional .

<?php
$a = array("India"=>array("KA","TN","KL"),"United States"=>array("AL","AK","AZ"));
?>