Specifications
echo “|”.$categories[$layer][$row][$column];
}
echo “|<BR>”;
}
}
Because of the way multidimensional arrays are created, we could create four-, five-, or six-
dimensional arrays. There is no language limit to the number of dimensions, but it is difficult
for people to visualize constructs with more than three dimensions. Most real-world problems
match logically with constructs of three or fewer dimensions.
Sorting Arrays
It is often useful to sort related data stored in an array. Taking a one-dimensional array and
sorting it into order is quite easy.
Using sort()
The following code results in the array being sorted into ascending alphabetical order:
$products = array( “Tires”, “Oil”, “Spark Plugs” );
sort($products);
Our array elements will now be in the order Oil, Spark Plugs, Tires.
We can sort values by numerical order too. If we have an array containing the prices of Bob’s
products, we can sort it into ascending numeric order as shown:
$prices = array( 100, 10, 4 );
sort($prices);
The prices will now be in the order 4, 10, 100.
Note that the sort function is case sensitive. All capital letters come before all lowercase letters.
So ‘A’ is less than ‘Z’, but ‘Z’ is less than ‘a’.
Using asort() and ksort() to Sort Associative Arrays
If we are using an associative array to store items and their prices, we need to use different
kinds of sort functions to keep keys and values together as they are sorted.
The following code creates an associative array containing the three products and their associ-
ated prices, and then sorts the array into ascending price order.
$prices = array( “Tires”=>100, “Oil”=>10, “Spark Plugs”=>4 );
asort($prices);
Using Arrays
C
HAPTER 3
3
USING ARRAYS
79
05 7842 CH03 3/6/01 3:42 PM Page 79