php arrays by justin nelsen. what is an array? - an array can store one or more values in a single...
TRANSCRIPT
![Page 1: 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](https://reader031.vdocuments.mx/reader031/viewer/2022020417/56649f155503460f94c2a771/html5/thumbnails/1.jpg)
PHP ArraysPHP Arrays
By Justin NelsenBy Justin Nelsen
![Page 2: 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](https://reader031.vdocuments.mx/reader031/viewer/2022020417/56649f155503460f94c2a771/html5/thumbnails/2.jpg)
What is an Array?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;
![Page 3: 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](https://reader031.vdocuments.mx/reader031/viewer/2022020417/56649f155503460f94c2a771/html5/thumbnails/3.jpg)
3 Kinds of Arrays3 Kinds of Arrays
1) Numeric Array2) Associative Array3) Multidimensional Array
1) Numeric Array2) Associative Array3) Multidimensional Array
![Page 4: 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](https://reader031.vdocuments.mx/reader031/viewer/2022020417/56649f155503460f94c2a771/html5/thumbnails/4.jpg)
Numeric ArrayNumeric 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.
![Page 5: 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](https://reader031.vdocuments.mx/reader031/viewer/2022020417/56649f155503460f94c2a771/html5/thumbnails/5.jpg)
Automatically Automatically
Example:
$names = array("Peter","Quagmire","Joe");
Example:
$names = array("Peter","Quagmire","Joe");
![Page 6: 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](https://reader031.vdocuments.mx/reader031/viewer/2022020417/56649f155503460f94c2a771/html5/thumbnails/6.jpg)
MunuallyMunually
Example:
$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";
Example:
$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";
![Page 7: 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](https://reader031.vdocuments.mx/reader031/viewer/2022020417/56649f155503460f94c2a771/html5/thumbnails/7.jpg)
The ID can be used in a script
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";
?>
![Page 8: 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](https://reader031.vdocuments.mx/reader031/viewer/2022020417/56649f155503460f94c2a771/html5/thumbnails/8.jpg)
OutputOutput
Quagmire and Joe are Peter's neighbors
![Page 9: 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](https://reader031.vdocuments.mx/reader031/viewer/2022020417/56649f155503460f94c2a771/html5/thumbnails/9.jpg)
Associative ArraysAssociative 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.
![Page 10: 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](https://reader031.vdocuments.mx/reader031/viewer/2022020417/56649f155503460f94c2a771/html5/thumbnails/10.jpg)
ExampleExample
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";
![Page 11: 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](https://reader031.vdocuments.mx/reader031/viewer/2022020417/56649f155503460f94c2a771/html5/thumbnails/11.jpg)
The Id can be used in a script
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.";
?>
![Page 12: 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](https://reader031.vdocuments.mx/reader031/viewer/2022020417/56649f155503460f94c2a771/html5/thumbnails/12.jpg)
OutputOutput
-“Brent is 42 years old.”
![Page 13: 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](https://reader031.vdocuments.mx/reader031/viewer/2022020417/56649f155503460f94c2a771/html5/thumbnails/13.jpg)
Multidimensional ArraysMultidimensional 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.
![Page 14: 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](https://reader031.vdocuments.mx/reader031/viewer/2022020417/56649f155503460f94c2a771/html5/thumbnails/14.jpg)
ExampleExample$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" )
);
![Page 15: 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](https://reader031.vdocuments.mx/reader031/viewer/2022020417/56649f155503460f94c2a771/html5/thumbnails/15.jpg)
OuputOuputArray([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?
![Page 16: 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](https://reader031.vdocuments.mx/reader031/viewer/2022020417/56649f155503460f94c2a771/html5/thumbnails/16.jpg)
ReferencesReferences
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