PHP Arrays
PHP Arrays
PHP array is an ordered map (contains value on the
basis of key). It is used to hold multiple values of similar type in a single
variable.
Advantage
of PHP Array
Less Code: We don't need to define
multiple variables.
Easy to traverse: By the help of single loop, we
can traverse all the elements of an array.
Sorting: We can sort the elements of
array.
PHP
Array Types
There
are 3 types of array in PHP.
- Indexed
Array
- Associative
Array
- Multidimensional
Array
PHP
Indexed Array
PHP
index is represented by number which starts from 0. We can store number, string
and object in the PHP array. All PHP array elements are assigned to an index
number by default.
There
are two ways to define indexed array:
1st
way:
1.
$season=array("summer","winter","spring","autumn");
2nd
way:
1. $season[0]="summer";
2. $season[1]="winter";
3. $season[2]="spring";
4. $season[3]="autumn";
Example
File:
array1.php
1.
<?php
2.
$season=array("summer","winter","spring","autumn");
3.
echo "Season are: $season[0], $season[1], $season[2] and $season[3]";
4.
?>
Output:
Season are: summer, winter, spring and autumn
File: array2.php
1. <?php
2. $season[0]="summer";
3. $season[1]="winter";
4. $season[2]="spring";
5. $season[3]="autumn";
6. echo "Season are: $season[0], $season[1], $season[2] and $season[3]";
7. ?>
Output:
Season are: summer, winter, spring and autumn
PHP
Associative Array
PHP allows you to
associate name/label with each array elements in PHP using => symbol. Such
way, you can easily remember the element because each element is represented by
label than an incremented number.
We can associate name with each array elements in
PHP using => symbol.
There are two ways to define associative array:
1st way:
1. $salary=array("Sonoo"=>"350000","John"=>"450000","Kartik"=>"200000");
2nd way:
1. $salary["Sonoo"]="350000";
2. $salary["John"]="450000";
3. $salary["Kartik"]="200000";
Example
File: arrayassociative1.php
1. <?php
2. $salary=array("Sonoo"=>"350000","John"=>"450000","Kartik"=>"200000");
3. echo "Sonoo salary: ".$salary["Sonoo"]."<br/>";
4. echo "John salary: ".$salary["John"]."<br/>";
5. echo "Kartik salary: ".$salary["Kartik"]."<br/>";
6. ?>
Output:
Sonoo
salary: 350000
John
salary: 450000
Kartik
salary: 200000
File: arrayassociative2.php
1. <?php
2. $salary["Sonoo"]="350000";
3. $salary["John"]="450000";
4. $salary["Kartik"]="200000";
5. echo "Sonoo salary: ".$salary["Sonoo"]."<br/>";
6. echo "John salary: ".$salary["John"]."<br/>";
7. echo "Kartik salary: ".$salary["Kartik"]."<br/>";
8. ?>
Output:
Sonoo salary: 350000
John salary: 450000
Kartik salary: 200000
PHP Multidimensional Array
PHP
multidimensional array is also known as array of arrays. It allows you to store
tabular data in an array. PHP multidimensional array can be represented in the
form of matrix which is represented by row * column.
Definition
1. $emp = array
2. (
3. array(1,"sonoo",400000),
4. array(2,"john",500000),
5. array(3,"rahul",300000)
6. );
PHP Multidimensional Array Example
Let's see
a simple example of PHP multidimensional array to display following tabular
data. In this example, we are displaying 3 rows and 3 columns.
Id |
Name |
Salary |
1 |
sonoo |
400000 |
2 |
john |
500000 |
3 |
rahul |
300000 |
File:
multiarray.php
1. <?php
2. $emp = array
3. (
4. array(1,"sonoo",400000),
5. array(2,"john",500000),
6. array(3,"rahul",300000)
7. );
8.
9. for ($row = 0; $row < 3; $row++) {
10. for ($col = 0; $col < 3; $col++) {
11. echo $emp[$row][$col]." ";
12. }
13. echo "<br/>";
14. }
15. ?>
Output:
1 sonoo 400000
2 john 500000
3 rahul 300000
Comments
Post a Comment