␡
- Accessing All Elements of Numeric Arrays
- Accessing All Elements of Associative Arrays
- Accessing All Array Elements in Nested Arrays
- Turning an Array into Variables
- Converting Strings to Arrays
- Converting Arrays to Strings
- Sorting Arrays Alphabetically
- Sorting Associative Arrays Alphabetically
- Sorting Nested Arrays
- Sorting Nested Associative Arrays
- Sorting IP Addresses (as a Human Would)
- Sorting Anything
- Sorting with Foreign Languages
- Applying an Effect to All Array Elements
- Filtering Arrays
- Getting Random Elements Out of Arrays
- Making Objects Behave Like Arrays
This chapter is from the book
Sorting Associative Arrays Alphabetically
ksort($a); asort($a);
<pre> <?php $a = array('one' => 'I', 'two' => 'II', 'three' => 'III', 'four' => 'IV'); ksort($a); print_r($a); asort($a); print_r($a); ?> </pre>
Sorting an Associative Array (sort_a.php)
Sorting associative arrays can be done in one of several ways:
- Sort by keys, leave key-value association intact: Use ksort().
- Sort by keys in reverse order, leave key-value association intact: Use krsort().
- Sort by values, leave key-value association intact: Use asort().
- Sort by values in reverse order, leave key-value association intact: Use arsort().
The preceding code shows these functions in action; Figure 2.3 shows the result.
Figure 2.3. Sorting associative arrays