How to change the order of array in php

王林
Release: 2023-03-12 09:32:02
Original
3089 people have browsed it

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);
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!