Skip to content
PHP Logo

What resources or examples can be found in the PHP manual to help with sorting arrays and manipulating elements?

When working with arrays in PHP, it is common to need to sort the elements or manipulate them in some way. The PHP manual provides a variety of functions and examples to help with these tasks. The `sort()` function can be used to sort an array in ascending order, while `rsort()` can be used to sort in descending order. Other functions like `array_push()` and `array_pop()` can be used to add or remove elements from an array.

php
// Sorting an array in ascending order
$numbers = array(4, 2, 8, 6);
sort($numbers);
print_r($numbers);

// Sorting an array in descending order
$letters = array('b', 'd', 'a', 'c');
rsort($letters);
print_r($letters);

// Adding an element to the end of an array
$fruits = array('apple', 'banana');
array_push($fruits, 'orange');
print_r($fruits);

// Removing the last element from an array
$colors = array('red', 'green', 'blue');
array_pop($colors);
print_r($colors);