Home >Backend Development >PHP Problem >How to change the order of array in php

How to change the order of array in php

王林
王林Original
2021-09-01 16:34:103124browse

The way PHP changes the order of an array is to use the array_reverse() function to flip the array, such as [$preserve=array_reverse($a,true);].

How to change the order of array in php

The operating environment of this article: windows10 system, php 7.1, thinkpad t480 computer.

In PHP we have many ways to change the order of an array, but one of the most convenient methods is to directly flip the array. PHP provides a function array_reverse() function, which can reverse the sequence of arrays easily and quickly.

Next let’s take a look at how to use this function.

Output the original array, flipped array, and flipped array retaining the original array key names:

Code implementation:

<?php
    $a=array("Volvo","XC90",array("BMW","Toyota"));
    $reverse=array_reverse($a);
    $preserve=array_reverse($a,true); 
    print_r($a);
    print_r($reverse);
    print_r($preserve);
?>

Friends can run the above code locally Down.

Recommended learning: php training

The above is the detailed content of How to change the order of array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn