How to keep key names after sorting PHP array by value?
The way to sort an array by value in PHP while retaining the key names is to use the usort() function to sort the array by value. The usort() function is passed an anonymous function as a comparison function that returns the difference in element values. usort() sorts the array according to an anonymous function while keeping the keys unchanged.
#How to sort an array by value in PHP and preserve key names?
In PHP, you can sort an array by value in ascending order using the asort()
function, or in descending order using the arsort()
function. However, both functions destroy the keys in the array.
If you want to sort the array by value while keeping the key names, you can use the usort()
function as follows:
$arr = array( 'a' => 10, 'b' => 5, 'c' => 20 ); usort($arr, function($a, $b) { return $a - $b; }); print_r($arr);
Output:
Array ( [b] => 5 [a] => 10 [c] => 20 )
In the above code:
usort()
accepts two parameters: the array to be sorted$arr
and an element value used to compare Anonymous function.- The anonymous function receives two elements
$a
and$b
and returns their difference. usort()
will sort the elements in the array, using an anonymous function as the comparison function.- After sorting, the key names in the array remain unchanged.
Practical case
Consider an array containing student grades:
$students = array( 'John' => 90, 'Jane' => 85, 'Peter' => 95 );
You want to sort the student names in ascending order by grade while keeping the name as Key name, you can use the following code:
usort($students, function($a, $b) { return $a - $b; });
Output:
Array ( [Jane] => 85 [John] => 90 [Peter] => 95 )
The above is the detailed content of How to keep key names after sorting PHP array by 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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

With the popularity of cryptocurrencies, virtual currency trading platforms have emerged. The top ten virtual currency trading platforms in the world are ranked as follows according to transaction volume and market share: Binance, Coinbase, FTX, KuCoin, Crypto.com, Kraken, Huobi, Gate.io, Bitfinex, Gemini. These platforms offer a wide range of services, ranging from a wide range of cryptocurrency choices to derivatives trading, suitable for traders of varying levels.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How to adjust Sesame Open Exchange to Chinese? This tutorial covers detailed steps on computers and Android mobile phones, from preliminary preparation to operational processes, and then to solving common problems, helping you easily switch the Sesame Open Exchange interface to Chinese and quickly get started with the trading platform.

The top ten cryptocurrency trading platforms include: 1. OKX, 2. Binance, 3. Gate.io, 4. Kraken, 5. Huobi, 6. Coinbase, 7. KuCoin, 8. Crypto.com, 9. Bitfinex, 10. Gemini. Security, liquidity, handling fees, currency selection, user interface and customer support should be considered when choosing a platform.

Top Ten Virtual Currency Trading Platforms 2025: 1. OKX, 2. Binance, 3. Gate.io, 4. Kraken, 5. Huobi, 6. Coinbase, 7. KuCoin, 8. Crypto.com, 9. Bitfinex, 10. Gemini. Security, liquidity, handling fees, currency selection, user interface and customer support should be considered when choosing a platform.
