PHP Tutorial : Arrays with Example

Leave a Comment

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 it's easy to define an array of 100 lengths. there are three different kinds of arrays and each array value is accessed using an ID which is called array index.

  • Numeric array  -  An array with a numeric index. Values are stored and accessed in linear fashion
  • Associative array  -  An array with strings as index. This stores element values in association with key value rather than in a strict linear index order.
  • Multidimensional array  -  An array containing one or more arrays and value are accessed using multiple indices.

Numeric array

These arrays can store numbers, strings and any object but their index will be represented by numbers.
By default array index starts from zero.

Example

<html>
<body>
<?php
/* First method to create array. */
$numbers=array(1,2,3,4,5);
foreach($numbers as $value)
{
echo "Value is $value <br/>";
}
/* Second method to create array.*/
$numbers[0]="one";
$numbers[1]="two";
$numbers[2]="three";
$numbers[3]="four";
$numbers[4]="five";
foreach($numbers as $value)
{   echo "Value is $value <br/>";
}
?>
</body>
</html>

Associative arrays

The associative arrays are very similar to numeric arrays in term of  functionality but they are different in terms of their index. Associative array will have their index as string so that you can  establish a strong association between key and values.

Example

<html>
<body>
<?php
/* First method to create array. */
$salaries=array(
"Amit" => 2000,
"Hemant" => 1000,
"Jatin" => 500
);
echo "Salary of Amit is".$salaries['Amit']."<br/>";
echo "Salary of Hemant is".$salaries['Hemant']."<br/>";
echo "Salary of Jatin is".$salaries['Jatin']."<br/>";
/* Second method to create array.*/
$salaries['Amit'] ="high";
$salaries['Hemant'] ="medium";
$salaries['Jatin'] ="low";
echo "Salary of Amit is".$salaries['Amit']."<br/>";
echo "Salary of Hemant is".$salaries['Hemant']."<br/>";
echo "Salary of Jatin is".$salaries['Jatin']."<br/>";
?>
</body>
</html>

Output:
Salary of Amit is 2000
Salary of Hemant is 1000
Salary of Jatin is 500
Salary of Amit is high
Salary of Hemant is medium
Salary of Jatin is low

Multidimensional arrays

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 indexes.

Two-dimensional Arrays

<?php
$shop=array(array("rose",1.25,15),
array("daisy",0.75,25),
array("orchid",1.15,7)
);
?>
<?php
echo "<h1>Manual access to each element</h1>";
echo $shop[0][0]. "costs".A$shop[0][1]. "and you get ".$shop[0][2]."<br
/>";
echo $shop[1][0]. "costs".A$shop[0][1]. "and you get ".$shop[1][2]."<br
/>";
echo $shop[2][0]. "costs".A$shop[0][1]. "and you get ".$shop[2][2]."<br
/>";
echo "<h1>Using loops to display array elements</h1>";
echo "<ol>";
for($row = 0; $row <3; $row++)
{
echo "<li><b>The row number $row</b>";
echo  "<ul>";
for($col = 0; $col < 3; $col++)
{
echo "<li>".$shop[$row][$col]."</li>";
}
echo "</ul>";
echo "</li>";
}
echo "</ol>";
?>

Three-dimensional Arrays

<?php
$shop = array (array("rose",1.25,15),
array("daisy",0.75,25),
array("orchid",1.15,7)
),
array(array("rose",1.25,15),
array("daisy",0.75,25),
array("orchid",1.15,7)
),
array(array("rose",1.25,15),
array("daisy",0.75,25),
array("orchid",1.15,7)
)
);
?>
<?php
echo "<ul>";
for($layer = 0; $layer <3; $layer++)
{
echo "<li>The layer number $layer";
echo "<ul>";
for($row = 0; $row <3; $row++)
{
echo "<li>The row number $row";
echo  "<ul>";
for($col = 0; $col < 3; $col++)
{
echo "<li>".$shop[$layer][$row][$col]."</li>";
}
echo "</ul>";
echo "</li>";
}
echo "</ul>";
echo "</li>";
}
echo "</ul>";
?>

Example

<html>
<body>
<?php
$marks=array(
"Amit" => array
(
"physics" => 35,
"maths" => 30,
"chemestry" => 39
),
"Hemant" => array
(
"physics" => 30,
"maths" => 32,
"chemestry" => 29
),
"Jatin" => array
(
"physics" => 31,
"maths" => 22,
"chemestry" => 39
)
);
/* Accessing multi-dimensional array values */
echo "Marks for Amit in physics:";
echo $marks['Amit']['physics']."<br/>";
echo "Marks for Hemant in maths:";
echo $marks['Hemant']['maths']."<br/>";
echo "Marks for Jatin in chemestry:";
echo $marks['Jatin']['chemestry']."<br/>";
?>
</body>
</html>

Output
Marks for Amit in physics : 35
Marks for Hemant in maths : 32
Marks for jatin in chemestry : 39

0 comments:

Post a Comment