Arrays are used to store many values in single variables.
Eg. Name of fruits
In Php, array can be declared as the following
Array($val1, $val2);
Please check the following screenshot
CODE
$fruitNames = array("Grapes","Banana","Papaya","Mango");
echo $fruitNames[0];
?>
OUTPUT IS
How to print an array?
$fruitNames = array("Grapes","Banana","Papaya","Mango");
echo "<pre>";
print_r($fruitNames);
?>
OUTPUT
How to count the number of elements of an array?
CODE
$fruitNames = array("Grapes","Banana","Papaya","Mango");
$count = count($fruitNames);
echo $count;
?>
OUTPUT
Dynamically how to fetch array values one by one.
CODE
$fruitNames = array("Grapes","Banana","Papaya","Mango");
foreach($fruitNames as $values)
{
echo $values."<br>";
}
?>
OUTPUT
LET’S DISCUSS ARRAY WITH ELEMENTS
$fruitNames = array("King1"=>"Grapes","King2"=>"Banana","King3"=>"Papaya","MainKing"=>"Mango");
echo "<pre>";
print_r($fruitNames);
?>
OUTPUT
Leave a comment