PHP Array@ Shwetank education.com
Definition –
An array is a special variable ,
Which can store multiple values in one single variable
Numeric Array in php
A numeric array stores each array element with a numeric index. There are two methods to create a numeric array.
The index are Automatically assigned(the index start at 0)
Syntax:
$hello=array(“jai”, “Veeru”, “Gabbar”);
Example
<?php
$car=array(“hello”, “Shwetank”);
echo “$car[1]”;
?>
We assign the index manually
Syntax:
$jai[0]=”Hum”;
$jai[1]=”Tum”;
$jai[2]= “Hai”;
Example
<?php
$sholey[0]=”Herry”;
$sholey[1]=”Anisha”;
$sholey[2]=”Gabbar”;
$sholey[3]=”Shwetank”;
echo $sholey[1]. “and”. $sholey[3]. “Love”;
?>
An Associative Array in php
With associative array we can use the values as keys and assign values to them.
<?php
$ages= array(“jai”=>25, “Veeru”=>24, “bsanti”=>22);
echo “$ages[jai]”;
?>
Multidimensional Array in php
In a 2 D array , each element in the main array can also be an array .
<?php
$employees[“employee 1”][“name”] = “Herry”;
$employees[“employee 1”][“title”] = “Owner”;
$employees[“employee 1”][“salary”] = “$60,000”;
$employees[“employee 2”][“name”] = “Anisha Gupta”;
$employees[“employee 2”][“title”] = “Manager”;
$employees[“employee 2”][“salary”] = “$40,000”;
$employees[“employee 3”][“name”] = “Shwetank Kumar Gupta”;
$employees[“employee 3”][“title”] = “Cashier”;
$employees[“employee 3”][“salary”] = “$30,000”;
echo $employees[“employee 1”][“name”].
” is the “.$employees[“employee 1”][“title”].
” and she earns “.$employees[“employee 1”][“salary”].
” a year.”;
?>
Try it Yourself