02.9a: Arrays

How do we scale if we have multiple variables of the same type? Naming each variable with a number at the end becomes hard to manage and will not scale ($name1, $name2, $name3,...). PHP offers arrays that let us have multiple values of the same type under one variable.

Note: The example I give in the text below is better than what is shown in the video.


https://www.youtube.com/watch?v=uNdbC9t0iZE





<?php
# * -------------------------------------------------------
# * Arrays

$name1 = "Eric";
$name2 = "James";
$name3 = "Jennifer";

print "name1: $name1\n";
print "name2: $name2\n";
print "name3: $name3\n";


if (strcmp( $name1, "James"))    {
    # * Do something...
}

if (strcmp( $name2, "James"))    {
    # * Do something...
}

if (strcmp( $name3, "James"))    {
    # * Do something...
}

# ...  Will not scale.



# * -------------------------------------------------------
# * Book definition of an array is a variable with multiple values.
# * Each array element has an index.  The index for the first element of the array is 0.
# * This is the example that is better in this text than in the video.

$names[0] = "Eric";
$names[1] = "James";
$names[2] = "Jennifer";
$names[3] = "Amber";


for ($i = 0; $i < 4; $i++)    			{
    if (strcmp( $names[$i], "James"))    {
        # * Do something...
    }
}


# * -------------------------------------------------------
for ($i = 0; $i < 4; $i++)    {
    print $names[$i] . "\n";
}



# * -------------------------------------------------------
# * Another way to create and initialize an array.

$names = array("Jenny", "Chris", "Ed", "Johan");

print_r( $names );



# * -------------------------------------------------------
# * Print the size of an array.

$array_size = sizeof( $names );

print "array_size: $array_size\n";



# * -------------------------------------------------------
# * Exercises:
# * 1) Create a program with two arrays: $name and $email_address, and print them both with a for loop.  
# * Instead of using a fixed value in the for(;conditional test;), use sizeof() to find the size of the first array.
# * (We will assume the arrays are the same size.)
/*
$names = array("Dave", "Eric", "Linda", "James", "Jennifer", "Lisa");
$emails = array("dave@fakeemail.com", "eric@fakeemail.com", "linda@fakeemail.com", "james@fakeemail.com", "jennifer@fakeemail.com", "lisa@fakeemail.com");
*/

# * 2) Create a program to find the highest number in an array.
# * $nums = array( 1, 12, 4, 14, 7, 5, 10, 9, 2, 6 );

# * 3) Create a program to calculate and display average temperature.  Put the temps in an array.
# * Recorded temperatures : 78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 75

# * 4) Create a program to read a list of names from a file and store the result in an array, then sort it, and print it.
# * Hint: Read about sort() on php.net
/* file.txt:
John
Bill
Dave
Jennifer
Bob
Larry
Amber
Danica
Gemma
Lisa
*/

# * 5) Create a program to check if an array contains a given number.  Return the index for that element if found,
# *    Otherwise return -1.



?>