PHP-Array Reverse

Source: Internet
Author: User

Array_reverse (returns an array of cells in reverse order)

Applicable (PHP 4, PHP 5)

Description

array_reverse   ( array   $array  [,  $preserve _keys

Array_reverse () accepts an array array as input and returns a new array of cells in reverse order.

Parameters:
    • array

    • The input array.

    • preserve_keys

    • If set to a TRUE key that retains numbers. Non-numeric keys are not affected by this setting and are always retained.

return value: Array ()

Returns the inverted array.

Example:

Example #1 Array_reverse () example


<?php
$input= Array ("PHP", 4.0, Array ("Green", "Red"));
$result= Array_reverse($input);
$result _keyed= Array_reverse($input, true);
?>


This will make $result and $result_keyed have the same unit, but note the difference between the key names.

$resultAnd $result_keyed The printout displays are:

Array ([0] = = Array ([0] = green [1] = + red) [1] = 4 [2] =&gt ; PHP) Array ([2] = = Array ([0] = green [1] = + red) [1] = 4 [0] = PHP)



Rsort (reverse sequence of an array)

Applicable (PHP 4, PHP 5)

Description

BOOL Rsort ( array &$array [, int $sort_flags = Sort_regular ] )

This function reverses the order of the array (highest to lowest).

Parameters:
    • array

    • The input array.

    • sort_flags

    • You can sort_flags change the ordering behavior with optional parameters, see sort () for details.

return value:

Returns when successful TRUE , or on failure FALSE .

Example:

Example #1 Rsort () example


<?php
$fruits= Array ("Lemon", "Orange", "Banana", "Apple");
Rsort($fruits);
foreach ($fruits as$key=$val) {
Echo"$key = $val\ n ";
}
?>


The above routines will output:


0 = Orange1 = Lemon2 = Banana3 = Apple

* Note: Fruits are sorted in alphabetical order.

Comments:

Note: This function array assigns the new key name to the element in. This will delete the original key name instead of just reordering the key name.


This article from "I am a phper, simple and naïve" blog, please be sure to keep this source http://thinkforphp.blog.51cto.com/8733331/1784646

PHP-Array Reverse

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.