An example program that creates and
prints an array (output ).
An
example program that creates and sorts and
prints a single array.(output )
An
example program that sorts arrays
, and times how long that takes.(output)
Keeping an array around between form executions source (run it here)
Arrays are accessed with square
brackets:
$names[4] = "Randy";
Arrays need not contain elements
of the same type. This is legal in PHP, but not in Java/C++
$names[0] = 1;
$names[2]
= "Two";
Arrays can be initalized without
using an index value. The default is one above the highest
used index.
$data[] = 0;
$data[]
= 1;
$data[] = 2;
$fred[10]
= 10;
$fred[] = 11; // Gets default
index 11
$fred[] = 12; // Gets default
index 12
Arrays can be initialized with the
array function
$countries = array("us",
"ca", "mx");
You can determine the number of
elements in the array with the count function:
$fred[1]
= 1;
$fred[100] = 100;
$c
= count($data); // Returns 2, not 100. Perl would return 100.
You can loop through arrays using
normal for loops, but this works on arrays with consecutive index
values only!
$c = count($data);
for($i
= 0; $i < $c; $i++) {
echo($data[$i]);
}
You can loop through using a
foreach
foreach ($array
as $value) {
// code
}
foreach ($array as $key =>
$value)
{
//
code
}
You can loop through arrays using
the current, key, reset, each, and list
functions. (OLD STYLE BAD, DON'T UNLESS YOU HAVE A REAL NEED)
This works always:
Every array has an element knows as the current element.
You can reset the current element to the begining of the array with reset($fred);
You can get the index value of the current element with key($fred);
You can get the value of the current element with current($fred);
You can get the move the current item one further down the
array with each. Each returns both the key and the value of
the new current element: list($key, $value) =
each($fred);
You can move one further down the list with
next, and one towards the front of the
list with prev
reset($fred);
echo(next($fred)); // prints 100
echo(prev($fred)); // prints 1;
Everything above still applies
Use square brackets, not curley
ones.
$countries["ca"] = "canada";
The array function works really well to assign values,
especially with the "=>" operator.
$countries
= array("canada" => "ca", "United
States" => us);
Arrays can be sorted with the sort and rsort functions.
Sorting an array with string
indexes moves the value and the index. Don't use sort on arrays with
string indexes, or non-consecutive indexes. The result will be
indexes like 0,1,2, ... and not what you want.
$data["one"]
= 1;
$data[100] = -100;
sorts
to
$data[0] = -100;
$data[1] = 1;
To sort and preserve index values,
use asort or arsort.
However, this can result in arrays not in index order for arrays
with numbered indexes.
$data["one"]
= 1;
$data[100] = -100;
sorts
to
$data[100] = -100;
$data["one"]
= 1;
To sort by a user defined function, try the usort command (example program) .
To unsort an array, use shuffle
shuffle($data);