Php Array| How To Use array PHP Briefly Details in 2021 |
Php Array| How To Use array PHP Briefly Details in 2021
PHP Array: It is a collection of data of the same or different data types. It is used to store groups of data simultaneously. It can hold many values under a single name, and you can access
the values by referring to an index number.
The indexing of the array always starts with 0, the array() function is used to create the array in PHP.
Initialization of Array Method in Php
$a=array(40,50,60,70,80); |
a[0] a[1] a[2] a[3] a[4]
40 | 50 | 60 | 70 | 80 |
How To Print Array Element in Php?
<?php $a=array(40,50,60,70,80); echo "Value at a[0]=".$a[0]."<br>"; echo "Value at a[1]=".$a[1]."<br>"; echo "Value at a[2]=".$a[2]."<br>"; echo "Value at a[3]=".$a[3]."<br>"; echo "Value at a[4]=".$a[4]."<br>"; ?> *****OUTPUT***** Value at a[0]=40 Value at a[1]=50 Value at a[2]=60 Value at a[3]=70 Value at a[4]=80 |
How To Print Array Element Using Loop in Php?
<?php $a=array(40,50,60,70,80); for($i=0;$i<=4;$i++) { echo "Value at a[".$i."]=".$a[$i]."<br>"; } ?> *****OUTPUT***** Value at a[0]=40 Value at a[1]=50 Value at a[2]=60 Value at a[3]=70 Value at a[4]=80 |
Indexed Array in PHP: It is a type of array in which each element of an array is associated
with the index value. It is also called a Numeric array. It can hold many values under a single name, and you can access
the values by referring to an index number. The indexing of the array
always starts with 0, the array() function is used to create an array in
PHP.
Initialization of Array Index Method in Php
$a=array([0] 40,[1] 50,[2] 60,[3] 70,[4] 80); |
a[0] a[1] a[2] a[3] a[4]
40 | 50 | 60 | 70 | 80 |
How To Print Index Array Element in Php?
?php $a=array(40,50,60,70,80); echo "Value at a[0]=".$a[0]."<br>"; echo "Value at a[1]=".$a[1]."<br>"; echo "Value at a[2]=".$a[2]."<br>"; echo "Value at a[3]=".$a[3]."<br>"; echo "Value at a[4]=".$a[4]."<br>"; ?> *****OUTPUT***** Value at a[0]=40 Value at a[1]=50 Value at a[2]=60 Value at a[3]=70 Value at a[4]=80 |
How To Print index Array Element Using Loop in Php?
<?php $a=array(40,50,60,70,80); for($i=0;$i<=4;$i++) { echo "Value at a[".$i."]=".$a[$i]."<br>"; } ?> *****OUTPUT***** Value at a[0]=40 Value at a[1]=50 Value at a[2]=60 Value at a[3]=70 Value at a[4]=80 |
Associative Array PHP: It is a type of array in which each element of the array is associated with the key.
In an associative array, there are two elements key and Value, array() function is used to create the array in PHP. There are two methods of creating an Associative array.
How To Use Associative Array in PHP?
<?php
$salary=array("Rohan"=>30000,"Rajesh"=>40000,"Mahesh"=>50000,"Rakesh"=>60000);
?> |
How To Use Associative index Array in PHP?
<?php $salary['Rohan'] = "30000"; $salary['Rajesh'] = "40000"; $salary['Mahesh'] = "50000"; $salary['Rakesh'] = "60000"; ?> |
How To Print Associative Array Element in Php?
<?php $salary=array("Rocky"=>35000,"Raj"=>50000,"Tony"=>45000,"Ronny"=>87000); echo "The Salary of Rocky is ".$salary['Rocky']."<br>"; echo "The Salary of Raj is ".$salary['Raj']."<br>"; echo "The Salary of Tony is ".$salary['Tony']."<br>"; echo "The Salary of Ronny is ".$salary['Ronny']."<br>"; ?> *****OUTPUT***** The salary of Rocky is 35000 The salary of Raj is 50000 The salary of Tony is 45000 The salary of Ronny is 87000 |
How To Print Associative Array Element Using Loop in Php?
<?php $salary=array("Rocky"=>35000,"Raj"=>50000,"Tony"=>45000,"Ronny"=>87000); foreach($salary as $k=>$v) { echo $k."'s Salary is ".$v."<br>"; } ?> *****OUTPUT***** Rocky's Salary is 35000 Raj's Salary is 50000 Tony's Salary is 45000 Ronny's Salary is 87000 |
Multidimensional Array: The array inside the array is called a multidimensional array. When an array contains one or more arrays then it
is called a Multidimensional array. array() function is used to create the array in PHP. It is called the PHP multidimensional array.
How To Use Multidimensional Array Using
indexed Array in Php?
<?php $matrix=array(array(80,50,20),array(70,40,10),array(90,60,30)); for($i=0;$i<=2;$i++) { for($j=0;$j<=2;$j++) { echo $matrix[$i][$j]." "; } echo "<br>"; } ?> *****OUTPUT***** 80 50 20 70 40 10 90 60 30 |
How To Use Multidimensional Array Using Associative array in Php?
<?php $student=array( "Rocky"=>array( "rollno"=>"205", "branch"=>"Computer science" ), "Raj"=>array( "rollno"=>"209", "branch"=>"Information technology" ), "Tony"=>array( " rollno"=>"206", "branch"=>"MCA Engineering" ) ); echo $student['Rocky']['rollno']."<br>"; echo $student['Rocky']['branch']."<br>"; echo $student['Tony']['rollno']."<br>"; echo $student['Tony']['branch']."<br>"; *****OUTPUT***** 205 Computer science 206 MCA Engineering |
Sorting of Array Elements
in PHP
Sort Method: Sort Method To Display
the element of the indexed array in ascending order.
sort() is the predefined function that is used to arrange the elements
of an array in ascending order.
How To Use Sort Method in Php?
<?php $no=array(80,25,69,51,20,37); echo "Before ascending<br>"; foreach($no as $v) { echo $v." "; } sort($no); echo "<br>After ascending<br>"; foreach($no as $v) { echo $v." "; } ?> *****OUTPUT***** Before ascending 80 25 69 51 20 37 After ascending 20 25 37 51 69 80 |
Resort Method: ReSort Method To Display
the element of the indexed array in descending order.
resort() is the predefined function that is used to arrange the elements
of an array in descending order.
How To Use Resort Method in Php?
<?php $no=array(80,25,69,51,20,37); echo "Before descending<br>"; foreach($no as $v) { echo $v." "; } rsort($no); echo "<br>After descending<br>"; foreach($no as $v) { echo $v." "; } ?> *****OUTPUT***** Before descending 80 25 69 51 20 37 After descending 80 69 51 37 25 20 |
Ksort Method: Ksort Method To Display
the element of the associative array in ascending order according to key
ksort() is the predefined function that is used to arrange the elements
of the associative array in ascending order according to key.
How To Use Ksort Method in Php?
<?php $age=array("Ravi"=>58,"Abhi"=>50,"Max"=>32); echo "Before ascending<br>"; foreach($age as $k=>$v) { echo $k." ".$v."<br>"; } ksort($age); echo "After ascending<br>"; foreach($age as $k=>$v) { echo $k." ".$v."<br>"; } ?> *****OUTPUT***** Before ascending Ravi 58 Abhi 50 Max 32 After ascending Abhi 50 Max 32 Ravi 58 |
Krsort Method: Krort Method To Display
the element of the associative array in descending order according to key
krsort() is a predefined function that is used to arrange the elements
of an associative array in descending order according to key.
How To Use Krsort Method in Php?
<?php $age=array("Ravi"=>58,"Abhi"=>50,"Max"=>32); echo "Before descending<br>"; foreach($age as $k=>$v) { echo $k." ".$v."<br>"; } krsort($age); echo "After descending<br>"; foreach($age as $k=>$v) { echo $k." ".$v."<br>"; } ?> *****OUTPUT***** Before descending Ravi 58 Abhi 50 Max 32 After descending Ravi 58 Max 32 Abhi 50 |
Asort Method: Asort Method To Display
the element of the associative array in ascending order according to value
asort() is a predefined function that is used to arrange the elements
of the associative array in ascending order according to value.
How To Use Asort Method in Php?
<?php $age=array("Ravi"=>58,"Abhi"=>50,"Max"=>32); echo "Before ascending<br>"; foreach($age as $k=>$v) { echo $k." ".$v."<br>"; } asort($age); echo "After ascending<br>"; foreach($age as $k=>$v) { echo $k." ".$v."<br>"; } ?> *****OUTPUT***** Before ascending Ravi 58 Abhi 50 Max 32 After ascending Max 32 Abhi 50 Ravi 58 |
Arsort Method: Arsort Method To Display the element of the associative array in descending order according to value
arsort() is a predefined function that is used to arrange the elements of an associative array in descending order according to value.
How To Use Arsort Method in Php?
<?php $age=array("Ravi"=>58,"Abhi"=>50,"Max"=>32); echo "Before descending<br>"; foreach($age as $k=>$v) { echo $k." ".$v."<br>"; } arsort($age); echo "After descending<br>"; foreach($age as $k=>$v) { echo $k." ".$v."<br>"; } ?> *****OUTPUT***** Before descending Ravi 58 Abhi 50 Max 32 After descending Ravi 58 Abhi 50 Max 32 |