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:
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.Code:$names = array("James","John","Robin","Sarah");
$names[] = "James";
$names[] = "John";
$names[] = "Robin";
Example:
Walking the ArrayCode:$names[5] = "James";
Since arrays use incremented subscripts you can use a loop to "walk through the array".
Example:
A few things to note here:Code:for ($i=0;$i<count($names);$i++) {
echo "$names[$i]<br />";
}
- 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:
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.Code:foreach ($names as $name) {
echo "$name<br />";
}
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:
So in this situation the key "james" is mapped to his age "15" and the key "john" is mapped to his age "30".Code:$people = array("james" => "15", "john" => "30", "sarah" => "20", "bob" => "19");
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:
Array FunctionsCode:function getNames() {
$names[] = "James";
$names[] = "John";
$names[] = "Chris";
return $names;
}
Merging Arrays
By using the array_merge function you can merge 2 or more arrays together.
Example:
Unique arraysCode:$names = array("james", "joe", "john");
$names2 = array("jill", "judith");
$names = array_merge($names,$names2);
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:
The array would now look like (1,2,3,5,8);Code:$nums = array(1,1,2,3,5,8);
$nums = array_unique($nums);
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:
SortingCode:$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.";
}
PHP has quite a few functions for sorting. These functions are:
- sort
- asort
- ksort
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:
This array now looks like:Code:$a = array("1","3","2","4","-2","6");
sort($a);
The keys in this situation have been reassigned. If you want to preserve the keys you want to use the ksort function.Code:$a = array("-2","1","2","3","4","6");
Example:
This array now looks like:Code:$names = array("james" => 15, "sarah" => 20,"john" => 12);
ksort($names);
Notice that the keys are in alphabetical order?Code:$names = array("james" => 15, "john" => 12, "sarah" => 20);
The asort function would sort by the values. If the values are strings the keys will be in ascending order.
Example:
This array now looks like:Code:$names = array("joe","bob","james","sarah","robin");
asort($names);
If this array was a numeric array it would be in ascending order.Code:$names = array("bob","james","joe","robin","sarah");
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
Last edited by chili5; 03-18-2009 at 05:57 PM.
See, if you keep doing this, you'll get popular and respected.
Another fantastic tutorial! Very nice, +rep!
Very nice. If you are interested in extending this, multi-dimensional arrays always throw people for a loop.
Edit: I see you wrote one in Java. You are ahead of me.![]()
Great! Another good idea to write about.![]()
ty..
thanks for the guide!
Your welcome.![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks