Arrays
Arrays are like a container to hold several variables. They are usually the first data structure taught to students. This is because an array is essentially the underlying data structure to the more complicated data structures such as the linked list, vector, and hash tables.
When you purchase a book on algorithms, it probably mentions something about data structures as well. The truth is data structures and algorithms go hand in hand. If you choose the wrong data structure for a problem, you may have a hard time getting a good implementation of your algorithm.
This tutorial is about 1-dimensional arrays in PHP and some of the functions for working with arrays.
As usual I will link to some problems for you to work on if you choose to. The best way to get good at programming is to write programs.
Topics
Defining Arrays
Walking The Array
Associative Arrays
Returning Arrays
Array Functions
In Array
Sorting
--------------
Defining Arrays
You can define an array in one of two ways. Using the array() function or the array operator ([]).
An array has a key and a value. So an array can easily function as a hashtable, and they are used to create hash tables.
Examples of defining arrays:
Code:
$names = array("James","John","Robin","Sarah");
$names[] = "James";
$names[] = "John";
$names[] = "Robin";
When you use the [] operator, the element is added to the next available location. You can also add to the array by including a subscript of where you want to add the element.
Example:
Code:
$names[5] = "James";
Walking the Array
Since arrays use incremented subscripts you can use a loop to "walk through the array".
Example:
Code:
for ($i=0;$i<count($names);$i++) {
echo "$names[$i]<br />";
}
A few things to note here:
- The first subscript of the array is 0.
- The count() function returns the number of elements in the array.
- The last subscript of the array is count()-1.
Foreach loop
You can also use a "foreach" loop to take a little walk through the array.
Example:
Code:
foreach ($names as $name) {
echo "$name<br />";
}
In this example, PHP takes a little walk through each element of the array. Each element of the array gets assigned one at a time to the $name variable. Once the walk is over, the $name variable no longer exists.
Associative Array
An Associative array is just an array where you choose the keys. The previous arrays keys were integers.
Example of declaring an associative array:
Code:
$people = array("james" => "15", "john" => "30", "sarah" => "20", "bob" => "19");
So in this situation the key "james" is mapped to his age "15" and the key "john" is mapped to his age "30".
Returning Arrays
In C++ you cannot explicitly return an array from a function. However in PHP you can. The syntax for return values from a function does not change.
Example:
Code:
function getNames() {
$names[] = "James";
$names[] = "John";
$names[] = "Chris";
return $names;
}
Array Functions
Merging Arrays
By using the array_merge function you can merge 2 or more arrays together.
Example:
Code:
$names = array("james", "joe", "john");
$names2 = array("jill", "judith");
$names = array_merge($names,$names2);
Unique arrays
If you want an array that is unique, with no repeated items what would you do? You could sort it and a linear sweep comparing S[i] to S[i+1] and building a new array would work. But there is a built in function for this purpose:
Code:
$nums = array(1,1,2,3,5,8);
$nums = array_unique($nums);
The array would now look like (1,2,3,5,8);
Is x in the array?
You can use the in_array function to determine if an element is in the array.
Given an array of the first 10 prime numbers, you want to determine if a random number is in the first 10 primes.
Solution:
Code:
$primes = array(2,3,5,7,9,11,13,17,19,23);
$rand = rand(1,20);
if (in_array($rand,$primes)) {
echo "$rand is in the first 10 primes.";
} else {
echo "$rand is not in the first 10 primes.";
}
Sorting
PHP has quite a few functions for sorting. These functions are:
The sort function doesn't try to keep the values of keys.
The asort function sorts by the values in the array.
The ksort function sorts by the keys.
Example:
Code:
$a = array("1","3","2","4","-2","6");
sort($a);
This array now looks like:
Code:
$a = array("-2","1","2","3","4","6");
The keys in this situation have been reassigned. If you want to preserve the keys you want to use the ksort function.
Example:
Code:
$names = array("james" => 15, "sarah" => 20,"john" => 12);
ksort($names);
This array now looks like:
Code:
$names = array("james" => 15, "john" => 12, "sarah" => 20);
Notice that the keys are in alphabetical order?
The asort function would sort by the values. If the values are strings the keys will be in ascending order.
Example:
Code:
$names = array("joe","bob","james","sarah","robin");
asort($names);
This array now looks like:
Code:
$names = array("bob","james","joe","robin","sarah");
If this array was a numeric array it would be in ascending order.
Have a look at w3schools for a great reference of what you can do with arrays:
PHP Array Functions
Now try a few problems:
The Trip
Vanilla Primes - use an array to solve this problem.
Stacks Of Blocks
Greedy Gift Givers
Deal Or No Deal scroll down to question 3.
Tough Being A Teen scroll down to question 4.
Now push yourself a little, with some sorting. Pinball Ranking watch the decimal places. Scroll to question 5.
Again take a good look at the documentation for your programming language to see just what you can do with built-in code.
Also if you decide to try the above problems make sure you read the specifications really closely. Remember it's all or nothing. 
Have fun,
James
Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum