0% found this document useful (0 votes)
39 views

Arrays

PHP arrays allow storing multiple values under a single variable name. There are three types of arrays in PHP: indexed arrays use numeric keys; associative arrays use named keys; and multidimensional arrays can contain other arrays. Indexed and associative arrays can be iterated through using for or foreach loops to access element values. Multidimensional arrays store arrays within other arrays, and their elements can be accessed using nested for or foreach loops.

Uploaded by

Kalash Shandilya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Arrays

PHP arrays allow storing multiple values under a single variable name. There are three types of arrays in PHP: indexed arrays use numeric keys; associative arrays use named keys; and multidimensional arrays can contain other arrays. Indexed and associative arrays can be iterated through using for or foreach loops to access element values. Multidimensional arrays store arrays within other arrays, and their elements can be accessed using nested for or foreach loops.

Uploaded by

Kalash Shandilya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

PHP 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:

• Indexed array — An array with a numeric key.


• Associative array — An array where each key has its
own specific value.
• Multidimensional array — An array containing one or
more arrays within itself.
Indexed Arrays
• An indexed or numeric array stores each array element with a
numeric index.

<?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);

for($x = 0; $x <$courseslength; $x++) {


echo $courses[$x];
echo "<br>";
}
?>
OUTPUT:
PHP
Laravel
Node js
Loop Through an Indexed Array(PHP foreach
Loop)
• The foreach loop is used to iterate over arrays.
• It is used to loop through each key/value pair in an array.
<?php
$courses = array("PHP", "Laravel", "Node js");

// Loop through colors array


foreach($courses as $course){
echo $course . "<br>";
}
?>

OUTPUT:
PHP
Laravel
Node js
Associative Array
• Associative arrays are arrays that use named keys that
you assign to them.

• We can associate name with each array elements in PHP


using => symbol.

• The keys assigned to values can be arbitrary and user


defined strings.
Associative Array(contd.)
<?php
$courses = array("INT220"=>"PHP", "INT221"=>"Laravel",
"INT222"=>"Node js");
echo "INT 220 is ".$courses['INT220'].". INT 221 is ".
$courses['INT221'].". INT222 is ".$courses['INT222'];
?>
OUTPUT:
INT 220 is PHP. INT 221 is Laravel. INT222 is Node js
Associative Array(contd.)
<?php
$courses["INT220"] = "PHP";
$courses["INT221"] = "Laravel";
$courses["INT222"] = "Node js";

// Printing array structure


print_r($courses);
?>

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
?>

You might also like