An array data structure, or simply an array, is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key. An array is stored so that the position of each element can be computed from its index

There are three basic types:

Indexed Array: Arrays with sequential numeric index, such as 0,1,2 etc.

Associative Array:This is the most frequently used type of PHP Arrays whose elements are defined in key/value pair.

Multidimensional Array: Arrays whose elements may contains one or more array.

Numeric Array

<?php
/*
Numeric Array 
*/
//Direct Method 
$arr[1] = 1;
$arr[2] = 2;
$arr[3] = 3;
foreach($arr as $value){
	echo "&nbsp ".$value;
}
//using Funcation Array 
$arr = array('1','2','3','4','5','6','7','8');
echo "<br>";
foreach ($arr as $value) {
	echo "&nbsp ".$value;
}
?>

OUTPUT

1  2  3
1  2  3  4  5  6  7  8

Associative Array

<?php
/*
Associative Array 
*/
//First Method 
//0 Id
$info["id"]=1;
$info["username"]="Arvind_Ahir";
$info["phone"]="95307";
$info["fname"]="Arvind";
$info["lname"]="Ahir";
//1 Id
foreach($info  as $val){
	echo "&nbsp ".$val;
}
?>

OUTPUT

1  Arvind_Ahir  95307  Arvind  Ahir

Multiple Array

<?php
/*
Multiple Array 
*/
$info["0"]["id"]=1;
$info["0"]["username"]="Arvind_Ahir";
$info["0"]["phone"]="9501S-ERVER";
$info["0"]["fname"]="Arvind";
$info["0"]["lname"]="Ahir";
//1 Id
$info["1"]["id"]=2;
$info["1"]["username"]="Abhi_ahir";
$info["1"]["phone"]="9502S-ERVER";
$info["1"]["fname"]="Abhi";
$info["1"]["lname"]="Ahir";
echo "<table border='1'>";
echo "<thead><tr><th>Sno</th><th>Username </th><th>Phone</th><th>Name </th></tr></thead>";
foreach($info  as $val){
	echo "<tr><td>".$val["id"]."</td><td>".$val["username"]."</td><td>".$val["phone"]."</td><td>".$val["fname"]." ".$val["lname"]."</td></tr>";
}
echo "</table>";
?>

OUTPUT

SnoUsernamePhoneName
1Arvind_Ahir9501S-ERVERArvind Ahir
2Abhi_ahir9502S-ERVERAbhi Ahir

 

(Visited 192 times, 1 visits today)
Share with Friends :
Written by: