Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP Arrays By Justin Nelsen. What is an Array? - An array can store one or more values in a single variable name. -Each element in the array is assigned.

Similar presentations


Presentation on theme: "PHP Arrays By Justin Nelsen. What is an Array? - An array can store one or more values in a single variable name. -Each element in the array is assigned."— 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; - 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 1)Numeric Array 2)Associative Array 3)Multidimensional Array 1)Numeric Array 2)Associative Array 3)Multidimensional Array

4 Numeric Array -A numeric array stores each element with a numeric ID key. -3 ways to write a 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"); Example: $names = array("Peter","Quagmire","Joe");

6 Munually Example: $names[0] = "Peter"; $names[1] = "Quagmire"; $names[2] = "Joe"; 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"; ?> 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. - 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"; 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."; ?> <?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. -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" ) ); $families = array ( "Griffin"=>array ( "Peter", "Lois", "Megan" ), "Quagmire"=>array ( "Glenn" ), "Brown"=>array ( "Cleveland", "Loretta", "Junior" ) );

15 Ouput Array ( [Griffin] => Array ( [0] => Peter [1] => Lois [2] => Megan ) [Quagmire] => Array ( [0] => Glenn ) [Brown] => Array ( [0] => Cleveland [1] => Loretta [2] => Junior ) 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 http://www.softwareprojects.org/php-arrays-08.htm http:www.w3schools.com/php/php_arrays.asp http://www.softwareprojects.org/php-arrays-08.htm


Download ppt "PHP Arrays By Justin Nelsen. What is an Array? - An array can store one or more values in a single variable name. -Each element in the array is assigned."

Similar presentations


Ads by Google