Array in PHP
Suppose we want to store 100 data item. So we need 100 different variable. So this is very typical work for programmer to manage 100 different variable.To overcome this problem we create a special type variable known as array.
array is a special type of variable that store one more values in one single variable. And each element in the array has its own index.
In PHP, there are three kind of arrays:
Numeric array - An array with a numeric index starting 0,1 etc.
Associative array - An array with a string index you can start like sun,mon,tue etc.
Multidimensional array - An array containing one or more arrays
1. we can create an array by array function.
<?php
$data=array("Sun","Mon","Tue","Wed");
?>
2. we can create each array element directly.
<?php
$data[0]="Sun";
$data[1]="Mon";
$data[2]="Tue";
$data[3]="Sun";
?>
A numeric array stores each array element with a numeric index.
In the following example the index are automatically assigned (the index starts at 0):
<?php
$data=array("Sun","Mon","Tue","Wed");
for($i=0;$i<4;$i++)
{
echo $data[$i]." ";
}
?>
Output:
Sun Mon Tue Web
array is a special type of variable that store one more values in one single variable. And each element in the array has its own index.
In PHP, there are three kind of arrays:
Numeric array - An array with a numeric index starting 0,1 etc.
Associative array - An array with a string index you can start like sun,mon,tue etc.
Multidimensional array - An array containing one or more arrays
1. we can create an array by array function.
<?php
$data=array("Sun","Mon","Tue","Wed");
?>
2. we can create each array element directly.
<?php
$data[0]="Sun";
$data[1]="Mon";
$data[2]="Tue";
$data[3]="Sun";
?>
A numeric array stores each array element with a numeric index.
In the following example the index are automatically assigned (the index starts at 0):
<?php
$data=array("Sun","Mon","Tue","Wed");
for($i=0;$i<4;$i++)
{
echo $data[$i]." ";
}
?>
Output:
Sun Mon Tue Web