Home  >  Article  >  Backend Development  >  How to implement two-dimensional array sorting in php

How to implement two-dimensional array sorting in php

青灯夜游
青灯夜游Original
2022-09-08 20:15:403370browse

In PHP, you can use the array_multisort() function to implement two-dimensional array sorting. This function can sort multiple arrays or multi-dimensional arrays, the syntax is "array_multisort (two-dimensional array, sort order, sort type)"; when the second parameter is omitted or set to "SORT_ASC", it will be sorted in ascending order and set to "SORT_DESC" Then sort in descending order.

How to implement two-dimensional array sorting in php

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

In php, you can use array_multisort () function implements two-dimensional array sorting.

<?php  
header("content-type:text/html;charset=utf-8");
$arr = array(5,3,array(2,4),1,array(3,6,1,0),-1); 
echo "原二维数组:";
var_dump($arr);

echo "排序后的二维数组:";
array_multisort($arr);
var_dump($arr);
?>

How to implement two-dimensional array sorting in php

This function sorts the outer elements first, and then sorts the inner subarray.

Description:

php built-in function array_multisort() is used to sort multiple arrays or multi-dimensional arrays. (Modify the original array)

You can enter one or more arrays. The function sorts the first array first, then the other arrays, and if two or more values ​​are the same, it sorts the next array.

array_multisort(array1,sorting order,sorting type,array2,array3...)
Parameters Description
array1 Required . Specifies an array.
sorting order Optional. Specify the order of sorting. Possible values:
  • SORT_ASC - Default. Sort in ascending order (A-Z).
  • SORT_DESC - Sort in descending order (Z-A).
sorting type Optional. Specifies the sorting type. Possible values:
  • SORT_REGULAR - Default. Put each item in regular order (Standard ASCII, don't change the type).
  • SORT_NUMERIC - Treat each item as a number.
  • SORT_STRING - Treat each item as a string.
  • SORT_LOCALE_STRING - Treat each item as a string, based on the current locale (can be changed via setlocale()).
  • SORT_NATURAL - Treat each item as a string, using natural sorting like natsort().
  • SORT_FLAG_CASE - Can be combined (bitwise ORed) with SORT_STRING or SORT_NATURAL to sort strings, case-insensitively.
array2 Optional. Specifies an array.
array3 Optional. Specifies an array.

#Note: String key names will be preserved, but numeric key names will be re-indexed, starting at 0 and increasing by 1.

Return value: TRUE if successful, FALSE if failed.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to implement two-dimensional array sorting in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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