PHP for the World Wide Web: Second Edition written by Larry Ullman
Chapter 7
Using Arrays
"Arrays are significantly different than numbers or strings, and you can't make the most of programming in PHP without them." p155
To Create an Array:
This exercise was to create the array of soups and then to notice the difference in results in using print and print_r to print them to the browser. Print simply shows the single word, Array. Print_r shows the list with its PHP structure.
Mmmmmmmm...soups:
Array
Array
(
[Monday] => Clam Chowder
[Tuesday] => White Chicken Chili
[Wednesday] => Vegetarian
)
The arrays below were created by using the range ().
I could only get them to work for the alphabet and numbering.
I had hoped they would do days of the week or months of the year, but they don't.
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
[5] => f
[6] => g
[7] => h
[8] => i
[9] => j
[10] => k
[11] => l
[12] => m
[13] => n
[14] => o
[15] => p
[16] => q
[17] => r
[18] => s
[19] => t
[20] => u
[21] => v
[22] => w
[23] => x
[24] => y
[25] => z
)
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
)
Things to remember in creating Arrays:
- If you use array() and list to define an index the others will follow in sequence
- Range() can be used to create an array based on a range of values
- Print and print_r have different effects on printing an array
- Arrays with numbers as keys are indexed arrays
- Arrays with strings as keys are associative arrays
Adding Items to an Array
In this exercise, the soup array script will be used to demonstrate the adding of elements to the list. A count was taken of the first array, and then a second count after the elements were added by using count().
The soups array originally had 3 elements.
After adding 3 more soups, the array now has 6 elements.
Array ( [Monday] => Clam Chowder [Tuesday] => White Chicken Chili [Wednesday] => Vegetarian [Thursday] => Chicken Noodle [Friday] => Tomato [Saturday] => Cream of Broccoli )To add elements brackets [] are used.
Merging Arrays
The soups script could be re-written with the array_merge() in these ways:
- $new_array = array_merge ($array1, array2);
- $new_array = $array1+array2;
- $array += $array2
This list of soups was merged with array_merge().
Array ( [Monday] => Clam Chowder [Tuesday] => White Chicken Chili [Wednesday] => Vegetarian [Thursday] => Chicken Noodle [Friday] => Tomato [Saturday] => Cream of Broccoli )Accessing Array Elements
There is only one way to access an element in an array and that is to refer to its key or index. The conflict of quotes comes up when trying to print a single element from an array. To combat that issue, the whole construct can be wrapped in curly braces. The next problem is the limitations placed on values/elements caused by the fact that an array can store multiple values. So, the best way to reach all the values in an array is to use the 'foreach' loop.
Here is a new soup script (using the old, though) with the foreach! Let's see what happens.
Monday: Clam Chowder
Tuesday: White Chicken Chili
Wednesday: Vegetarian
Thursday: Chicken Noodle
Friday: Tomato
Saturday: Cream of Broccoli
Does this mean I finally understand the foreach???
Creating Multidimensional Arrays
Hmmm, on p 169, it is said "Multidimensional arrays are both simple and complicated at the same time." They are a great way to display more information than the standard array. That is becasue you can use other arrays instead of strings and values.
Here is a script for a multidimensional array holding several chapters of three books.
"My Books and Chapters"
The third chapter of my first book is HTML Forms and PHP.
Wow! It works! let's try another one.
The fourth chapter of my third book is Introduction to SQL and MySQL.
I will stop here although it is fun to choose the information this way, for example, the second chapter in my second book is Object Oriented Programming.
Now let's try a foreach to list out the books.
PHP VQS: Array
PHP Advanced: Array
PHP and MySQL: Array
Now, to list the books and the chapters using the nested foreach.
PHP VQS
Chapter 1 is Getting Started
Chapter 2 is Variables
Chapter 3 is HTML Forms and PHP
Chapter 4 is Using Numbers
PHP Advanced
Chapter 1 is Advanced PHP Programming
Chapter 2 is Object Oriented Programming
Chapter 3 is Databases
Chapter 4 is Security
PHP and MySQL
Chapter 1 is Introduction to PHP
Chapter 2 is Programming with PHP
Chapter 3 is Creating Dynamic Websites
Chapter 4 is Introduction to SQL and MySQL
Wow!
Sorting Arrays
There are a variety of ways to sort and sorting can be numerical or alphabetical. Because each element of an array is a pair (key and value)you can chose to keep the keys as is and sort the values or keep the values and sort the keys.
| Functions | Sorts by | Keeps Key Values |
|---|---|---|
| sort() | values | No |
| rsort() | values, inverse | No |
| asort() | values | Yes |
| arsort() | values, inverse | Yes |
| ksort() | keys | Yes |
| krsort() | keys, inverse | Yes |
Here is a sorting exercise.
Originally the array looks like this:
Bobby: 95
Alex: 83
Clement: 98
Christina: 65
Manny: 75
Melany: 88
Kaitlin: 71
After sorting the array by value using arsort(),
the array looks like this:
Clement: 98
Bobby: 95
Melany: 88
Alex: 83
Manny: 75
Kaitlin: 71
Christina: 65
Now to sort the array by key using ksort(),
the array looks like this:
Alex: 83
Bobby: 95
Christina: 65
Clement: 98
Kaitlin: 71
Manny: 75
Melany: 88
Powerful!
Transforming Between Strings and Arrays
I didn't completely undewrstand the reasons for using these functions which are written:
- implode() to turn an array into a string
- explode() to turn a string into an array
Here is the syntax for explode():
$array = explode ($separator, $string);
or the separator:
$array = explode (',', $string);
or
$array = (' ', $string);
Here is the syntax for implode():
$string = implode ($glue, $array);
or the glue:
$string = implode (',', $array);
or $string = implode (' ', $array);
To Convert Between Strings and Arrays
Because the the explode(), implode() functions are so simple, you can quickly and easily sort a submitted list of words with just a couple of lines. (paraphrased from p 181)
I Must Sort This Out!
Enter the words you want to alphabetize with each word separated by a space from the others:I used implode to turn the sorted array back into a string but could have used a foreach loop or join() function.
Creating an Array from a Form
The exercises thus far have been creating arrays within a PHP page. This will be different in that it will create an array from input to an HTML Form. This will be shown with a set of checkboxes. Here we go!
