PHP Arrays
PHP Arrays
An array stores multiple values in one single
variable.
An array is a special variable, which can hold more
than one value at a time.
An array can hold many values under a single
name, and you can access the values by referring to
an index number.
Create an Array in PHP
In PHP, the array() function is used to create an array:
array();
In PHP, there are three types of arrays:
• Indexed arrays - Arrays with a numeric
index
• Associative arrays - Arrays with named
keys
• Multidimensional arrays - Arrays
containing one or more arrays
PHP Indexed Arrays
Thereare two ways to create indexed
arrays:
Theindex can be assigned automatically
(index always starts at 0), like this:
$cars = array("Volvo", "BMW", "Toyota");
or the index can be assigned manually:
$cars[0]= "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
<!DOCTYPE html>
<html>
<body>
<?php
$myArr = array("GLA", "University", "Mathura");
echo "I am doing BCA from " . $myArr[0] . " " . $ myArr[1] . "," . $ myArr[2] . ".";
?>
</body>
</html>
Output:
I am doing BCA from GLA University,Mathura.
<!DOCTYPE html> To loop through and print all
the values of an indexed
<html> array, you could use
<body> a for loop, like this:
<?php
$myArr = array("GLA", "University", Output:
"Mathura");
$arrlength = count($myArr); GLA
for($x = 0; $x < $arrlength; $x++) {
University
echo $myArr[$x]; Mathura
echo "<br>";
}
?>
</body>
</html>
PHP Associative Arrays
Associative arrays are arrays that use named keys that
we assign to them.
There are two ways to create an associative array:
$age = array(“Rohit"=>“23", “Kapil"=>“19",
“Neeraj"=>“20");
or:
$age[‘Rohit'] = “23";
$age[‘Kapil'] = “19";
$age[‘Neeraj'] = “20";
The named keys can then be used in a script.
<!DOCTYPE html>
<html>
<body>
<?php
$age = array("Rohit"=>"23", "Kapil"=>"19", "Neeraj"=>"20");
echo "Kapil is " . $age['Kapil'] . " years old.";
?>
</body>
</html>
Output:
Kapil is 19 years old.
<!DOCTYPE html>
<html> Output:
<body>
Key=Rohit, Value=23
<?php
Key=Kapil, Value=19
$age = array("Rohit"=>"23",
"Kapil"=>"19", "Neeraj"=>"20"); Key=Neeraj, Value=20
foreach($age as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
</body>
</html>
PHP Multidimensional Arrays
A multidimensional array is an array containing one
or more arrays.
PHP supports multidimensional arrays that are two,
three, four, five, or more levels deep.
However, arrays more than three levels deep are
hard to manage for most people.
Name Class Roll No
Romya BCA 1
Madhur BCA 2
Vishal BCA 3
Kirat BCA 4
We can store the data from the table above in a two-
dimensional array, like this:
$std = array (
array(“Romya",BCA,1),
array(“Madhur",BCA,2),
array(“Vishal",BCA,3),
array(“Kirat",BCA,4)
);
Now the two-dimensional $std array contains four arrays, and
it has two indices: row and column.
To get access to the elements of the $cars array we must point to the two
indices (row and column):
<!DOCTYPE html>
<html>
<body>
<?php
$std = array ( Romya: is in : BCA. Her Roll No. is: 18.
array("Romya","BCA",18), Madhur: is in: BCA. His Roll No. is: 13.
array("Madhur","BCA",13), Vishal: is in: BCA. His Roll No. is: 2.
array("Vishal","BCA",2), Kirat: is in: BCA. Her Roll No. is: 15.
array("Kirat","BCA",15)
);
echo $std[0][0].": is in : ".$std[0][1].". Her Roll No. is: ".
$std[0][2].".<br>";
echo $std[1][0].": is in: ".$std[1][1].". His Roll No. is: ".
$std[1][2].".<br>";
echo $std[2][0].": is in: ".$std[2][1].". His Roll No. is: ".
$std[2][2].".<br>";
echo $std[3][0].": is in: ".$std[3][1].". Her Roll No. is: ".
$std[3][2].".<br>";
?>
<!DOCTYPE html>
<html>
<body>
Row number 0 <?php
•Romya $std = array (
•BCA array("Romya","BCA",18),
•18 array("Madhur","BCA",13),
Row number 1 array("Vishal","BCA",2),
•Madhur array("Kirat","BCA",15)
•BCA );
•13
Row number 2 for ($row = 0; $row < 4; $row++) {
•Vishal echo "<p><b>Row number $row</b></p>";
•BCA echo "<ul>";
•2 for ($col = 0; $col < 3; $col++) {
Row number 3 echo "<li>".$std[$row][$col]."</li>";
•Kirat }
•BCA echo "</ul>";
•15 }
?>
</body>
</html>
PHP - Sort Functions For Arrays
•sort() - sort arrays in ascending order
•rsort() - sort arrays in descending order
•asort() - sort associative arrays in ascending order, according to the value
•ksort() - sort associative arrays in ascending order, according to the key
•arsort() - sort associative arrays in descending order, according to the value
•krsort() - sort associative arrays in descending order, according to the key
PHP Array Functions
1) PHP array() function
PHP array() function creates and returns an array. It allows you to create
indexed, associative and multidimensional arrays.
<?php
$season=array("summer","winter","spring","autumn");
echo "Season are: $season[0], $season[1], $season[2] and $season[3]";
?>
Output:
Season are: summer, winter, spring and autumn
PHP Array Functions
2) PHP array_change_key_case() function:
PHP array_change_key_case() function changes the case of all key of an array.
Note: It changes case of key only.
Syntax
array array_change_key_case ( array $array [, int $case = CASE_LOWER ] )
Example
<?php
$salary=array("Sno"=>"550000","Vimal"=>"250000","Ratan"=>"200000");
print_r(array_change_key_case($salary,CASE_UPPER));
?>
Output:
Array ( [SNO] => 550000 [VIMAL] => 250000 [RATAN] => 200000 )
PHP Array Functions
Example
<?php
$salary=array("Sonoo"=>"550000","Vimal"=>"250000","Ratan"=>"200000
"); print_r(array_change_key_case($salary,CASE_LOWER));
?>
Output:
Array ( [sonoo] => 550000 [vimal] => 250000 [ratan] => 200000 )
PHP Array Functions
3) PHP array_chunk() function
PHP array_chunk() function splits array into chunks.
By using array_chunk() method, you can divide array into many parts.
Syntax
array array_chunk ( array $array , int $size [, bool $preserve_keys = false ] )
Example
<?php
$salary=array("Sonoo"=>"550000","Vimal"=>"250000","Ratan"=>"200000");
print_r(array_chunk($salary,2));
?>
Output:
Array ( [0] => Array ( [0] => 550000 [1] => 250000 ) [1] => Array ( [0] => 200000 ) )
PHP Array Functions
4) PHP count() function
PHP count() function counts all elements in an array.
Example
<?php
$season=array("summer","winter","spring","autumn");
echo count($season);
?>
Output:
4
PHP Array Functions
5) PHP sort() function
PHP sort() function sorts all the elements in an array.
Example
<?php
$season=array("summer","winter","spring","autumn");
sort($season);
foreach( $season as $s )
{
echo "$s";
}
?>
Output:
autumn spring summer winter
PHP Array Functions
6) PHP array_reverse() function
PHP array_reverse() function returns an array containing elements in reversed order.
Example
<?php
$season=array("summer","winter","spring","autumn");
$reverseseason=array_reverse($season);
foreach( $reverseseason as $s )
{
echo "$s";
}
?>
Output:
autumn spring winter summer
PHP Array Functions
7) PHP array_search() function
PHP array_search() function searches the specified value in an array. It returns key if
search is successful.
Example
<?php
$season=array("summer","winter","spring","autumn");
$key=array_search("spring",$season);
echo $key;
?>
Output:
2
8) PHP array_intersect() function
PHP array_intersect() function returns the intersection of two array.
In other words, it returns the matching elements of two array.
Syntax
array array_intersect ( array $array1 , array $array2 [, array $... ] )
Example
<?php
$name1=array("sonoo","john","vivek","smith");
$name2=array("umesh","sonoo","kartik","smith");
$name3=array_intersect($name1,$name2);
foreach( $name3 as $n )
{
echo "$n<br />";
}
?>
Output:
Sonoo
smith