CourseLecture PHP Arrays
CourseLecture PHP Arrays
PHP Arrays
What is an Array?
Arrays are complex variables that allow us to store
more than one value or a group of values under a
single variable name.
An array is a special variable, which can hold more
than one value at a time.
Types of Array in PHP
• 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.
Syntax: array();
Indexed Array
An indexed or numeric array stores each array element with a
numeric index.
Example #1:
Php Script Output
<?php
$teams = array("Nuggets", "Lakers", "Bulls", "Bucks");
// Printing array structure
print_r($teams);
?>
Indexed Array
Example #2:
Php Script Output
<?php
$teams[0] = “Nuggets";
$teams[1] = “Lakers";
$teams[2] = “Bulls";
$teams[3] = “Bucks";
Parameters:
value
- The expression to be printed.
return
- If you would like to capture the output of print_r(), use the return
parameter. When this parameter is set to true, print_r() will return the
information rather than print it.
Associative Array
In an associative array, the keys assigned to values can be arbitrary and user
defined strings.
Example:
Php Script Output
<?php
$nba["Jordan"] = "23";
$nba["Bryant"] = "24";
$nba["O Neal"] = "32";
$nba["Curry"] = "30";
<?php Array
(
$nba = array( [0] => Array
(
[0] => Jordan
// Default key for each will start from 0 [1] => Bryant
array("Jordan", "Bryant", "James"), )
[2] => James
array("Curry", "Butler", "Booker")
); [1] => Array
(
[0] => Curry
// Display the array information [1] => Butler
[2] => Booker
print_r($nba); )
?> )
End of Presentation