Type of array in PHP
- Numeric Array.
- Associative Array.
- Multidimensional Array.
Numeric Array: -
- In numeric array store each element with a numeric index.There are generally two methods to create a numeric array:
- $computer = array("Acer”,”Lenovo”,”deal”,”hp”); In this the index value are automatically assigned to each array element starts from 0.
- $computer[0] = “Acer”; $computer[1] = “Lenovo”; $computer[2] = “deal”; $computer[3] = “hp”; In this we assign the index value to each element in array.
Example:
<?php
$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 : <?php
$employee = array (“Ram”=>23,”Sham”=>24,”Pankaj”=>23); Or
$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
)
)
No comments:
Post a Comment