Getting Started
Basics
Arrays
Loop/Branch
Functions
OOP
Libraries
Files
MySQL
SQL
PHP/MySQL
Regex
Email
Date/Time
Web
Forms
Working With Arrays
Working with Numeric Keys
Working with Associative Arrays
Assigning Values to Arrays
Creating a Multidimensional Array
Searching Arrays
Sorting Arrays
Removing Values from Arrays
Measuring Array Differences
Merging Arrays
LAB: Using an Array to Generate a Drop Down Menu
Working with Numeric Keys
What is a key?
A "key," also referred to as an "index,"
is how you can reference an array element or value
What numeric data types can be used as keys?
Only positive integers can be used in numeric array keys
What is the starting number for a numeric array?
Numeric array keys start with 0 by default
How can numeric arrays be accessed?
Use $array[N] where N = a positive integer
Working with Associative Arrays
What data types can be used as keys?
Strings are used as associative array keys
How can associative arrays be accessed?
Use $array["string"] where "string" is a key you create
Assigning Values to Arrays
How can initial array assignments be made?
range() for numeric arrays or array() for any array
What about assigning keys?
array(key => value, key => value)
How can you add to the end of an array?
$array[] = value;
Can you add keys out of order?
keys can be assigned in any order
Creating a Multidimensional Array
How can you initialize a multidimensional array?
array(array(), array(), etc.) can be used for initialization
How can you make a single assignment?
$array[][] = value
How do you access a single cell in a multidimensional array?
echo $array[key][key]
Searching Arrays
How do you search an array by value?
in_array() and array_search()
How do you search an array by key?
array_key_exists() or isset($array["key"])
How do you find the length of an array?
count($array)
Sorting Arrays
How do you sort an array by value?
sort() or asort()
How do you sort an array by key?
ksort()
How do you sort an array by value but in reverse?
rsort() or arsort()
How do you sort a multidimensional array?
array_multisort()
Removing Values from Arrays
How do you remove a single array element?
unset($array[xxx])
How do you remove an array element from the end?
array_pop()
How do you remove an array element from the beginning?
array_shift()
Measuring Array Differences
How do you measure the difference between 2 arrays?
array_diff()
How do you copy a subsection of an array?
array_slice()
Merging Arrays
How do you merge arrays together?
array_merge()
LAB: Using an Array to Generate a Drop Down Menu
From the WorkingFiles folder for this chapter ...
In your editor open the file arrays_project_titles.php
Note that it defines an array $titles
In your editor open htdocs/sweetscomplete/products.php
Copy the array $titles from arrays_project_titles.php into products.php
In your editor close the file arrays_project_titles.php
Create a dropdown list of products using the array $titles as follows ...
Locate <form name="search" ... >
After <input type="text" add open and close <select ... > tags
Inside the <select ... > tags insert PHP tags
Create a "foreach () { }" loop
Inside the brackets { } use printf() to build an <option> tag for each array element
Test in your browser
http://localhost/sweetscomplete/products.php
Sort the array and refresh the browser page
Insert "sort($titles, SORT_STRING)" before the foreach loop
Create links between products.php and index.php
+
o
-