Array: An array is a variable,which can store multiple values in a single variable with same data type. A data type may be number or string or any data type.
In PHP, there are three kind of arrays:
Numeric array - An array with a numeric index
Associative array - An array where each ID key is associated with a value
Multidimensional array - An array containing one or more arrays
1) Numeric arrary example:
<?php
$course=Array("php","java","dotnet","C++");
echo "$course[0]<br/>";
echo "$course[1]<br/>";
echo "$course[2]<br/>";
echo "$course[3]<br/>";
?>
Out Put:
php
java
dotnet
C++
2)Associative array
<?php
$course=array("java"=>jsp,"dotnet"=>asp,"c++"=>oops);
echo "java is basic for" .$course['java']."learning<br/>";
foreach($course as $cat=>$dept)
{
echo $cat."=>".$dept;
echo "<br/>"; }
?>
?>
<?php
$course=array("java"=>jsp,"dotnet"=>asp,"c++"=>oops);
echo "java is basic for" .$course['java']."learning<br/>";
foreach($course as $cat=>$dept)
{
echo $cat."=>".$dept;
echo "<br/>"; }
?>
?>
Out put:
java is basic forjsplearning
java=>jsp
dotnet=>asp
c++=>oops
java is basic forjsplearning
java=>jsp
dotnet=>asp
c++=>oops
3)Multidimensional array<?php
$books = array(
array('Engineering','Civil', 'ComputerScience','Mechanical'),
array('Management','Hospitality','HumanResource','Quality'),
);?>
<table border="1"><tr>
<th>Category Name</th>
<th>Dept-1</th>
<th>Dept-2</th>
<th>Dept-3</th> </tr>
<?php
foreach($books as $cat){
echo '<tr>';
foreach($cat as $dept) {
echo "<td>$dept</td>"; }
echo '</tr>';}
?>
Out put:
$books = array(
array('Engineering','Civil', 'ComputerScience','Mechanical'),
array('Management','Hospitality','HumanResource','Quality'),
);?>
<table border="1"><tr>
<th>Category Name</th>
<th>Dept-1</th>
<th>Dept-2</th>
<th>Dept-3</th> </tr>
<?php
foreach($books as $cat){
echo '<tr>';
foreach($cat as $dept) {
echo "<td>$dept</td>"; }
echo '</tr>';}
?>
Out put:
Category Name | Dept-1 | Dept-2 | Dept-3 |
---|---|---|---|
Engineering | Civil | ComputerScience | Mechanical |
Management | Hospitality | HumanResource | Quality |