Home > Backend Development > PHP Problem > php array sorted by key

php array sorted by key

藏色散人
Release: 2023-02-24 07:04:01
Original
22674 people have browsed it

php array sorted by key

How to sort php array by key

ksort() - Sort the array in ascending order according to the key of the associated array

krsort() - Sort the array in descending order based on the key of the associative array

ksort() - Sort the array in ascending order based on the key of the array

Below Example of Sort an associative array in ascending order based on the array's key:

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
ksort($age);
?>
Copy after login

Output:

Array ( [Ben] => 37 [Joe] => 43 [Peter] => 35 )
Copy after login

krsort() - Sort the array in descending order based on the array's key

The following example sorts the associative array in descending order according to the key of the array:

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
krsort($age);
?>
Copy after login

Output:

Array ( [Peter] => 35 [Joe] => 43 [Ben] => 37 )
Copy after login

Related recommendations: "PHP Tutorial"

The above is the detailed content of php array sorted by key. 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