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 "  ".$value; } //using Funcation Array $arr = array('1','2','3','4','5','6','7','8'); echo "<br>"; foreach ($arr as $value) { echo "  ".$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 "  ".$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
Sno | Username | Phone | Name |
---|---|---|---|
1 | Arvind_Ahir | 9501S-ERVER | Arvind Ahir |
2 | Abhi_ahir | 9502S-ERVER | Abhi Ahir |
(Visited 209 times, 1 visits today)
Written by: