Title: Arrays ($$$)
Course: PHP/MySQL Course

Creating indexed Arrays

Read about arrays in the text book from page 149 to 151

Notes:

- These arrays in this part of the book all have a numeric index
- Here are three was to create the same array:

  1. $prices[] = 12.22;
$prices[] = 15.05;
$prices[] = 56.95;

  2. $prices[0] = 12.22;
$prices[1] = 15.05;
$prices[2] = 56.95;

  3. $prices = array( 12.22, 15.05, 56.95);

Each of these creates an array with three elements $price[0], $price[1] and $price[2]. Each of these elements refers to a place in memory and can be used anywhere a normal variable can be used.
- arrays with numeric indexes do not have to start at 0, for example:

$prices[1] = 12.22;
$prices[2] = 15.05;
$prices[3] = 56.95;

- Note, when using array elements in a string in, you should surround the element with { } like this:

echo "Price 1 is {$price[1]}, and price 2 is {$price[2]}";

Question:
Given this code,what will it print out?

$a[] = 10;
$a[] = 20;
$a[] = 30;
echo $a[1]+$a[2];

- 50

Associative arrays

Play the video at KillerPHP.com: Arrays part 2, This is a better introduction to associative arrays than the book.

Question:

What operator goes between the key and the value when you create an associative array

- ->
- =>
- ::
- >>

looping on arrays

There are several ways too loop through the elements of an array:

1. If the array only had numeric keys, the you can use a for loop like this:

$days = array( 'Mon', 'Tue', 'Wed', 'Thr', 'Fri', 'Sat', 'Sun');

for($i = 0; $i < $days.count(); $i++) {
echo "$i is {$days[$i]}<br />";
}

2. if the array has indexed or key/value pairs, then you can use a special loop:

$prices = array(
'TV' => 450.00,
'Radio' => 120.00,
'Watch' => 56.95);

foreach( $prices as $p) { // this sets a variable to a copy of the array element each time though the loop
echo "$p <br />";
}

or if you want a loop that looks up each key/value pair and allows you to reference them inside the loop:

foreach( $prices as $k => $p ) { // this form sets a variable to the key and value each time though
echo "The price of $k is $p<br \>";
}

3. The books shows the following way of looping through an array of key/values:

while( list($k,$v) = each($prices)) {
echo "$k costs $v <br />";
}

This form of looping was available in the older versions of php, and modern programs are encourages to use the foreach form of the loop to be more compatible to how other languages do it.

The only advantage to the while look in #3 is that you can modify or even delete array entries while you go though the loop, the $v actually points to the real array element, where in the foreach loop, the value only points to a copy of the data in memory. Although if you read the official language reference page on foreach (ref foreach) you will see there is a variation that allows you to modify the original array, but you do not need this very often.

Question:

What do you type into the blank to make this code loop though and type out all the numbers in the loop?

$nums = array( 1, 2, 3, 5, 8, 13 );

_________ ( $nums as $n ) {
echo " $n";
}

- foreach

Array functions and Multidimensional

Please read page 154 and 155 about functions that work on arrays. You can also glance at this link: ref array functions.

Multidimensional arrays:

Any array and have elements that are other arrays, so to an array of rows and columns to represent the following table:

id | name | color
101 | Bob | green
102 | Gerry | red
103 | Mary | blue

you could write the following:

$table = array(
array( "id" => 101, "name" => "Bob", "color" => "green"), // row 0
array( "id" => 102, "name" => "Gerry", "color" => "red"), // row 1
array( "id" => 103, "name" => "Mary", "color" => "blue") // row 2
);

You can reference an element by doing something like this:

echo $table[1]['name'];

you could change a color by doing this:

$table[2]['color'] = 'yellow';

Also note that when you create an array, if for any element you leave out the key in the key value pair, it just assigns the next available number as the key. So

$list = array ( 100, "blue"=>200, 300, "red"=>400, 2);
actually creates the following array elements:

$list[0] is 100
$list['blue'] is 200
$list[1] is 300
$list['red'] is 400
$list[2] is 500

since you can mix types in arrays in php, you can have any arbitrary arrangement of elements being arrays or other data, this is perfectly legal and can be useful for a particular problem:

$things = array(
3,
"religious books" => array( "Bible", "Koran", "Tanakh"),
"SciFi" => array(
"books" => array( "Beyond Mars", "Electric Dreams"),
"DVD" => array( "Alien", "Mars", "Abyss", "Solar Quest")
)
);

The print_r() function:

When debugging arrays, it is nice to be able to easily see what is in them with a simple print statment:

The print_r function will print any variable into a human readable output, and is especially good for arrays:

If you use:

echo print_r($things);

on the array $things above and then display your web page result as source you will see a nicely formatted output.

Try it out by going to http://cbis3.lbcc.edu/tstudent/topic2/things.php
and then viewing as source.

Question:

True or False: You are allowed to have arrays as elements in other arrays?

- true
- false