PHP ARRAY
Like another programming language php also having array. Array is a special variable which can used to store multiple values of same data type.
Array is used to hold all values under one single name and we used that value by referring that name of array. Array can be accessed by its index value.
Type of array in PHP
- 1.Numeric Array.
- 2.Associative Array.
- 3.Multidimensional Array
- Numeric Array: - In numeric array store each element with a numeric index.
There are generally two methods to create a numeric array:
- a)$computer = array("Acer”,”Lenovo”,”deal”,”hp”);
In this the index value are automatically assigned to each array element starts from 0.
- b)$computer[0] = “Acer”;
- c)$computer[1] = “Lenovo”;
- d)$computer[2] = “deal”;
- e)$computer[3] = “hp”;
In this we assign the index value to each element in array.
Example:
$computer[0] = “Acer”;
$computer[1] = “Lenovo”;
$computer[2] = “ deal”;
$computer[3] = “hp”;
Echo $computer[0].”and”.$computer[1].”are cheap and best”;
?>
Output: Acer and Lenovo are cheap and best
- Associative Array: In this each id is associative with a value.
Numeric array is not best in storing data about specific named so we can use associative array. With associative array we can use the value as key and assign to them.
Example :
$employee = array (“Ram”=>23,”Sham”=>24,”Pankaj”=>23);
Or
$employee [‘Ram’] =”23”;
$employee [‘Sham’] = “24”;
$employee [‘Pankaj’] = “23”;
Example:
$employee [‘Ram’] =”23”;
$employee [‘Sham’] = “24”;
$employee [‘Pankaj’] = “23”;
echo “Ram is”.$employee[‘Ram’].”Year old”;
?>
Output: Ram is 23 year old.
- Multidimensional Array: in this an array element contain array or we can say that each element in the main array can also be an array. And each element in the sub-array can be an array.
Example:
Array
(
[kumar]=> array
(
[0] =>pankaj
[1]=>amit
)
[singh]=> array
(
[0]=> sumit
)
)