(or)
Array is a collection of types.The array can hold any type.It can contain strings,integers,floating points and even other arrays.Each of these values inside an array can be accessed with a related key value pair.An array can be created in two ways.array()
or array[]
.Array() function can be used to create an array in PHP.
Indexed arrays
– These arrays has a numeric indexAssociative arrays
– These arrays has named keysMultidimensional arrays
– These arrays has one or more arrays.1) The assignment of index is done automatically.
[php]
<?php
// Define an indexed array
$names = array("John", "Mike", "Brake");
?>
[/php]
2) The assignement of index is done manually.
[php]
<?php
$colors[0] = "John";
$colors[1] = "Mike";
$colors[2] = "Brake";
?>
[/php]
[php]
<!DOCTYPE html>
<html>
<body>
<?php
$age = array("John"=>"24", "Mike"=>"16", "Brake"=>"26", "Hussey"=>"26");
echo "John Age : ".$age[‘John’]."<br/>";
echo "Mike Age : ".$age[‘Mike’]."<br/>";
echo "Brake Age : ".$age[‘Brake’]."<br/>";
echo "Hussey Age : ".$age[‘Hussey’]."<br/>";
?>
</body>
</html>
[/php]
Output:
[php]
<!DOCTYPE html>
<html>
<body>
<?php
$details = array ( array("Peter","B.Tech",2012), array("John","B.Tech",2010), array("Harry","M.Tech",2013) );
echo "Name : ".$details[0][0]." -> Qualification: ".$details[0][1].", year: ".$details[0][2].".<br/>";
echo "Name : ".$details[1][0]." -> Qualification: ".$details[1][1].", year: ".$details[1][2].".<br/>";
echo "Name : ".$details[2][0]." -> Qualification: ".$details[2][1].", year: ".$details[2][2].".<br/>";
?>
</body>
</html>
[/php]
Output: