Php array_reverse returns the array instance code in reverse order, and the array_reverse Array

Source: Internet
Author: User

Php array_reverse returns the array instance code in reverse order, and the array_reverse Array

The php array_reverse function returns an array with the opposite unit order. This function has two parameters. The first parameter indicates the array to be processed, and the second parameter is optional, specifying whether to retain the key name of the original array. This article describes how to use the array_reverse function through examples. For more information about the coders, see.

Basic syntax of the array_reverse function:

array array_reverse ( array $array [, bool $preserve_keys = false ] )

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

If the second parameter is set to true, the key name of the element remains unchanged; otherwise, the key name is lost.

Parameter Introduction

Parameters Description
Array Required. Input array.
Preserve

Optional. Specifies whether to retain the key name of the original array. Returns an array with the opposite unit order. If it is set to TRUE, the numeric key is retained. Non-numeric keys are not affected by this setting and are always retained.

Possible values:

  • True
  • False

Return Value

Returns the inverted array.

Instance:

<?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 pay attention to the key name difference. The output of $ result and $ result_keyed are:

Array( [0] => Array (  [0] => green  [1] => red ) [1] => 4 [2] => php)Array( [2] => Array (  [0] => green  [1] => red ) [1] => 4 [0] => php)

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.