Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

CourseLecture PHP Arrays

Uploaded by

jay.abaleta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

CourseLecture PHP Arrays

Uploaded by

jay.abaleta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Course Learning

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";

// Printing array structure


print_r($teams);
?>
print_r in PHP
• print_r — Prints human-readable information about a variable
Description:
print_r(mixed $value, bool $return = false): string|bool

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";

// Printing array structure


print_r($nba);
?>
Multidimensional Array
 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.
 Syntax:
array (
array (elements...),
array (elements...),
...
)
Multidimensional Array
 Example:
Php Script Output

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

You might also like