An array is a data structure that store multiple values in single variable. In PHP to create an array, array() function is used.
<?php
$mobile = array("Samsung", "Nokia", "Micromax"); //$mobile is a single variable that contain many similar types of values.
echo "I like " . $mobile[0] . ", " . $mobile[1] . " and " . $mobile[2] . ".";
?>
Indexed array:
These array can store numbers, object and strings but their index is represented by numbers. Default value of any index is 0.
<?php
$mobile = array("Samsung", "Nokia", "Micromax");
foreach($mobile as $k=>$v)
echo("$k:$v.</br>");
?>
Associative arrays:
Note: These types of array are not kept inside double quotes else it will not return any value.
<?php
$salary = array("ragini"=>"35000", "Shikha"=>"40000", "Megha"=>"45000");
echo "Shikha salary is " . $salary['Shikha'];
?>
<?php
$record = array(
"Rakesh" => array (
"Hindi" => 52,
"English" => 34,
"History" => 33
),
"salman" => array (
"Hindi" => 39,
"English" => 35,
"History"=> 34
),
"Shikha" => array (
"Hindi" => 37,
"English" => 29,
"History" => 31
)
);
/* Accessing multi-dimensional array values */
echo "Marks for Rakesh in History : " ;
echo $record['Rakesh']['History'] . "<br />";
echo "Marks for salman in English : ";
echo $record['salman']['English'] . "<br />";
echo "Marks for Shikha in Hindi : " ;
echo $record['Shikha']['Hindi'] . "<br />";
?>
Leave Comment
1 Comments
View All Comments