Home Backend Development PHP Tutorial How to sort associative array in PHP? (code example)

How to sort associative array in PHP? (code example)

Nov 07, 2018 am 10:13 AM
php Associative array sorting

This article mainly introduces how to use PHP to sort associative arrays.

For PHP learners, arrays are a very important knowledge point. The so-called array is the ability to store one or more values ​​​​in separate variable names. An indexed array is an array with a numeric index, an associative array is an array with a specified key, and a multidimensional array is an array containing one or more arrays.

Below we will introduce to you various sorting methods of associative arrays through simple examples.

The code example is as follows:

<?php
echo "Associative array : Ascending order sort by value";
$array2=array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"40"); 
asort($array2);
foreach($array2 as $y=>$y_value)
{
echo "Age of ".$y." is : ".$y_value."
";
}
echo "Associative array : Ascending order sort by Key";
$array3=array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"40"); 
ksort($array3);
foreach($array3 as $y=>$y_value)
{
echo "Age of ".$y." is : ".$y_value."";
}
echo "Associative array : Descending order sorting by Value";
$age=array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"40");
arsort($age);
foreach($age as $y=>$y_value)
{
echo "Age of ".$y." is : ".$y_value."";
}
echo "Associative array : Descending order sorting by Key";
$array4=array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"40"); 
krsort($array4);
foreach($array4 as $y=>$y_value)
{
echo "Age of ".$y." is : ".$y_value."
";
} 
?>
Copy after login

The output result is as follows:

1. In ascending order by value

Associative array : Ascending order sort by value   
Age of Sophia is : 31                                       
Age of William is : 39                                      
Age of Ramesh is : 40                                       
Age of Jacob is : 41
Copy after login

2. Sort the associative array in ascending order by key name:

Associative array : Ascending order sort by Key    
Age of Jacob is : 41                                        
Age of Ramesh is : 40                                       
Age of Sophia is : 31                                       
Age of William is : 39
Copy after login

3. Sort in descending order by value

Associative array : Descending order sorting by Value 
Age of Jacob is : 41                                        
Age of Ramesh is : 40                                       
Age of William is : 39                                      
Age of Sophia is : 31
Copy after login

4. Sort the associative array in descending order according to the key name:

Associative array : Descending order sorting by Key 
Age of William is : 39                                      
Age of Sophia is : 31                                       
Age of Ramesh is : 40  
Age of Jacob is : 41
Copy after login

Introduction to related functions:

arsort() Function pair association The array is sorted in descending order by key value.

asort() The function sorts the associative array in ascending order by key value.

krsort() The function sorts the associative array in descending order by key name.

ksort() The function sorts the associative array in ascending order by key name.

This article is an introduction to the sorting method of associative arrays. I hope it will be helpful to friends in need!

The above is the detailed content of How to sort associative array in PHP? (code example). For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

See all articles