PHP array_reverse() Function
In this chapter you will learn:
- Definition for PHP array_reverse() Function
- Syntax for PHP array_reverse() Function
- Parameter for PHP array_reverse() Function
- Example for PHP array_reverse() Function
- Example - Preserve the keys during the reverse
Definition
The array_reverse() function returns an array in the reverse order.
Syntax
PHP array_reverse() Function has the following syntax.
array_reverse(array,preserve)
Parameter
Parameter | Is Required | Description |
---|---|---|
array | Required. | Array to reverse |
preserve | Optional. | If to preserve the keys of the array or not. |
Possible values for preserve:
- true
- false
Example 1
Reverse an associate array
<?php
$a=array("a"=>"A","b"=>"Java","c"=>"java2s.com");
print_r(array_reverse($a));
?>
The code above generates the following result.
Example 2
Preserve the keys during the reverse
<?php/*from j ava 2 s . c o m*/
$a=array("Java","PHP",array("HTML","CSS"));
$reverse=array_reverse($a);
$preserve=array_reverse($a,true);
print_r($a);
print_r($reverse);
print_r($preserve);
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP array_search() Function
- Syntax for PHP array_search() Function
- Parameter for PHP array_search() Function
- Example for PHP array_search() Function
- Example - Search an array for the value 5 and return its key
Home » PHP Tutorial » PHP Array Functions