Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP Arrays By Justin Nelsen.

Similar presentations


Presentation on theme: "PHP Arrays By Justin Nelsen."— Presentation transcript:

1 PHP Arrays By Justin Nelsen

2 What is an Array? -An array can store one or more values in a single variable name. -Each element in the array is assigned its own ID so that it can be easily accessed. -$array[key] = value;

3 3 Kinds of Arrays Numeric Array Associative Array
Multidimensional Array

4 Numeric Array A numeric array stores each element with a numeric ID key. 3 ways to write a numeric array.

5 Automatically Example: $names = array("Peter","Quagmire","Joe");

6 Munually Example: $names[0] = "Peter"; $names[1] = "Quagmire";
$names[2] = "Joe";

7 The ID can be used in a script
Example: <?php $names[0] = "Peter"; $names[1] = "Quagmire"; $names[2] = "Joe"; echo $names[1] . " and " . $names[2] . " are ". $names[0] . "'s neighbors"; ?>

8 Output Quagmire and Joe are Peter's neighbors

9 Associative Arrays -An associative array, each ID key is associated with a value. -When storing data about specific named values, a numerical array is not always the best way to do it. -With associative arrays we can use the values as keys and assign values to them.

10 Example Using an array to assign an age to a person.
$ages = array(”Brent"=>42, ”Andrew"=>25, "Joshua”16=>); $ages[’Brent'] = ”42"; $ages[’Andrew'] = ”25"; $ages['Joshua'] = ”16";

11 The Id can be used in a script
<?php $ages[‘Brent’] = ”42"; $ages[‘Andrew’] = ”25"; $ages[‘Joshua’] = ”16"; echo Brent is " . $ages[‘Brent’] . " years old."; ?>

12 Output -“Brent is 42 years old.”

13 Multidimensional Arrays
In a multidimensional array, each element in the main array can also be an array. - And each element in the sub-array can be an array, and so on.

14 Example $families = array ( "Griffin"=>array "Peter", "Lois",
"Megan" ), "Quagmire"=>array ( "Glenn" ), "Brown"=>array "Cleveland", "Loretta", "Junior" ) );

15 Ouput Is Megan a part of the Griffin family?
Array ( [Griffin] => Array [0] => Peter [1] => Lois [2] => Megan ) [Quagmire] => Array [0] => Glenn [Brown] => Array [0] => Cleveland [1] => Loretta [2] => Junior echo "Is " . $families['Griffin'][2] . " a part of the Griffin family?"; Is Megan a part of the Griffin family?

16 References http:www.w3schools.com/php/php_arrays.asp


Download ppt "PHP Arrays By Justin Nelsen."

Similar presentations


Ads by Google