


How to sort the php array in reverse order without changing the key value
How to implement reverse sorting of key values in PHP arrays: 1. Use the rsort() function, with the syntax "rsort(array, sortingtype)"; 2. Use the arsort() function, with the syntax "arsort(array, sortingtype)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php array implementation key Sort the value array in descending order
1. Use the rsort() function
rsort() function to sort the numerical array in descending order.
<?php header("Content-type:text/html;charset=utf-8"); $arr = array(2, 1, 5, 16, 29, 15); echo "原数组:"; var_dump($arr); echo "倒序排序后:"; rsort($arr); var_dump($arr); ?>
2. Use the arsort() function
The arsort() function sorts the associative array in descending order by key value.
<?php header("Content-type:text/html;charset=utf-8"); $age = array("张三"=>30,"李四"=>23,"王五"=>15,"李华"=>12,"娜娜"=>26,"小红"=>16); arsort($age); var_dump($age); arsort($age,2); var_dump($age); ?>
Note:
rsort() function and arsort() function both support two parameters, the second parameter can If omitted, you can specify how the elements/items of the array are arranged. Possible values:
0 = SORT_REGULAR - Default. Put each item in regular order (Standard ASCII, don't change the type).
1 = SORT_NUMERIC - treat each item as a number.
2 = SORT_STRING - Treat each item as a string.
3 = SORT_LOCALE_STRING - Treat each item as a string, based on the current locale (can be changed via setlocale()).
4 = SORT_NATURAL - Treat each item as a string, using natural sorting like natsort().
5 = SORT_FLAG_CASE - Can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings, case-insensitively.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to sort the php array in reverse order without changing the key value. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
