Arrays
Arrays
PHP arrays
• Arrays are complex variables that allow us to store more
than one value or a group of values under a single
variable name.
Types of Arrays in PHP
There are three types of arrays that you can create. These
are:
<?php
$courses = array("PHP", "Laravel", "Node js");
echo "I like " . $courses[0] . ", " . $courses[1] . " and " . $courses[2];
echo "<br>";
echo count($courses);
?>
OUTPUT:
I like PHP, Laravel and Node js
3
Loop Through an Indexed Array(for loop)
<?php
$courses = array("PHP", "Laravel", "Node js");
$courseslength = count($courses);
OUTPUT:
PHP
Laravel
Node js
Associative Array
• Associative arrays are arrays that use named keys that
you assign to them.
OUTPUT:
Array ( [INT220] => PHP [INT221] => Laravel [INT222] =>
Node js )
Loop Through an Associative Array(for each
loop)
<?php
$courses =
array("INT220"=>"PHP","INT221"=>"Laravel","INT222"=>"Node
js");
foreach($courses as $course => $value) {
echo "Key=".$course.","."Value=".$value;
echo "<br>";
}
?>
OUTPUT:
Key=INT220, Value=PHP
Key=INT221, Value=Laravel
Key=INT222, Value=Node js
Loop Through an Associative Array(for loop)
<?php
$courses = array('INT220'=>'PHP','INT221'=>'Laravel','INT222'=>'N
ode js');
$keys = array_keys($courses);
$values = array_values($courses);
for($x=0; $x<count($courses); $x++) {
echo "Key=".$keys[$x].','."Value=".$values[$x]. "<br>";
}
?>
OUTPUT:
Key=INT220,Value=PHP
Key=INT221,Value=Laravel
Key=INT222,Value=Node js
Multidimensional Arrays
• The multidimensional array is an array in which each
element can also be an array and each element in the
sub-array can be an array or further contain array within
itself and so on.
Multidimensional Arrays(contd.)
OUTPUT:
<?php Manoj----CGPA is: 7.8 and his status is
$result = array( pass
array("Manoj",7.8,"pass"), Aditya----CGPA is: 8.5 and his status is
pass
array("Aditya",8.5,"pass"),
Anuj----CGPA is: 4.9 and his status is fail
array("Anuj",4.9,"fail")
);
echo $result[0][0]. "----CGPA is: " . $result[0][1]." and his status is ".
$result[0][2]."<br>";
echo $result[1][0]. "----CGPA is: " . $result[1][1]." and his status is ".
$result[1][2]."<br>";
echo $result[2][0]. "----CGPA is: " . $result[2][1]." and his status is ".
$result[2][2];
?>
Multidimensional Arrays(contd.)
<?php
$result = array(
array(
"name" => "Manoj",
OUTPUT:
"cgpa" => 7.8, Manoj----CGPA is: 7.8 and his status is pass
),
"status" => "pass"
Aditya----CGPA is: 8.5 and his status is pass
array( Anuj----CGPA is: 4.9 and his status is fail
"name" => "Aditya",
"cgpa" => 8.5,
"status" => "pass"
),
array(
"name" => "Anuj",
"cgpa" => 4.9,
"status" => "fail"
)
);
echo $result[0]["name"]. "----CGPA is: " . $result[0]["cgpa"]." and his status is ".$result[0]["status"]."<br>";
echo $result[1]["name"]. "----CGPA is: " . $result[1]["cgpa"]." and his status is ".$result[1]["status"]."<br>";
echo $result[2]["name"]. "----CGPA is: " . $result[2]["cgpa"]." and his status is ".$result[2]["status"];
?>
Loop Through an Multidimensional Array(for
loop)
<?php
$result = array ( OUTPUT:
array("Manoj",7.8,"pass"),
array("Aditya",8.5,"pass"), Row number 0
array("Anuj",4.9,"fail") Manoj
7.8
Pass
);
Row number 1
Aditya
for ($row = 0; $row < 3; $row++) {
8.5
echo "<h4>Row number $row</h4>"; Pass
for ($col = 0; $col < 3; $col++) {
Row number 2
echo $result[$row][$col]."<br>";
Anuj
} 4.9
} fail
?>
Loop Through an Multidimensional Array(foreach
loop)
<?php
OUTPUT:
$result = array (
array("Manoj",7.8,"pass"),
array("Aditya",8.5,"pass"), Row number 0
Manoj
array("Anuj",4.9,"fail") 7.8
); Pass
for($row = 0; $row < 3; $row++) { Row number 1
echo "<h4>Row number $row</h4>"; Aditya
8.5
Pass
foreach ($result[$row] as $resul) {
echo $resul."<br>"; Row number 2
Anuj
} 4.9
} fail
?>
Loop Through an Multidimensional Array(foreach
loop)
<?php
$books =
array("C++" => array("name" => "Beginning with C","copies" =>8),
"PHP" => array("name" => "Basics of PHP","copies" => 10),
"Laravel" => array("name" => "MVC","copies" => 3)
);
C++
name = Beginning with C
$keys = array_keys($books); copies = 8
for($i = 0; $i < count($books); $i++) {
PHP
echo "<h1>$keys[$i]</h1>"; name = Basics of PHP
foreach($books[$keys[$i]] as $key => $value) { copies = 10
echo $key . " = " . $value . "<br>";
Laravel
} name = MVC
} copies = 3
?>