Home Backend Development PHP Tutorial A simple method to implement multi-dimensional array sorting in PHP

A simple method to implement multi-dimensional array sorting in PHP

Dec 21, 2016 am 09:08 AM
Array sort

The example in this article describes how to simply implement multi-dimensional array sorting in PHP. Share it with everyone for your reference, the details are as follows:

When I was doing a function before, I had to put the data into a two-dimensional array and sort it, and then go online to find solutions.

The array_multisort function will be used at this time. The array_multisort() function sorts multiple arrays or multi-dimensional arrays

Let’s look at an example first

<?php
$data=array(
0=>array(&#39;one&#39;=>34,&#39;two&#39;=>&#39;d&#39;),
1=>array(&#39;one&#39;=>45,&#39;two&#39;=>&#39;e&#39;),
2=>array(&#39;one&#39;=>47,&#39;two&#39;=>&#39;h&#39;),
3=>array(&#39;one&#39;=>12,&#39;two&#39;=>&#39;c&#39;),
4=>array(&#39;one&#39;=>15,&#39;two&#39;=>&#39;w&#39;),
5=>array(&#39;one&#39;=>85,&#39;two&#39;=>&#39;r&#39;),
);
foreach($data as $val){
$key_arrays[]=$val[&#39;one&#39;];
}
array_multisort($key_arrays,SORT_ASC,SORT_NUMERIC,$data);
var_dump($data);
Copy after login

The output result: Sort by key value one, as follows:

array
 0 => 
  array
   &#39;one&#39; => int 12
   &#39;two&#39; => string &#39;c&#39; (length=1)
 1 => 
  array
   &#39;one&#39; => int 15
   &#39;two&#39; => string &#39;w&#39; (length=1)
 2 => 
  array
   &#39;one&#39; => int 34
   &#39;two&#39; => string &#39;d&#39; (length=1)
 3 => 
  array
   &#39;one&#39; => int 45
   &#39;two&#39; => string &#39;e&#39; (length=1)
 4 => 
  array
   &#39;one&#39; => int 47
   &#39;two&#39; => string &#39;h&#39; (length=1)
 5 => 
  array
   &#39;one&#39; => int 85
   &#39;two&#39; => string &#39;r&#39; (length=1)
Copy after login

php Multi-dimensional array sort

The following is encapsulated into a function for easy use

function my_array_multisort($data,$sort_order_field,$sort_order=SORT_ASC,$sort_type=SORT_NUMERIC){
foreach($data as $val){
$key_arrays[]=$val[$sort_order_field];
}
array_multisort($key_arrays,SORT_ASC,SORT_NUMERIC,$data);
rturn $data;
}
Copy after login

I hope this article will be helpful to everyone in PHP programming.

For more articles on how to simply implement multi-dimensional array sorting in PHP, please pay attention to 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

Video Face Swap

Video Face Swap

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

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)

Fast array sorting method that preserves key names in PHP Fast array sorting method that preserves key names in PHP May 02, 2024 pm 03:06 PM

Fast array sorting method in PHP that preserves key names: Use the ksort() function to sort the keys. Use the uasort() function to sort using a user-defined comparison function. Practical case: To sort an array of user IDs and scores by score while retaining the user ID, you can use the uasort() function and a custom comparison function.

JS array sorting: in-depth analysis of the working principle and mechanism of the sort() method JS array sorting: in-depth analysis of the working principle and mechanism of the sort() method Dec 28, 2023 am 11:47 AM

To deeply understand JS array sorting: the principles and mechanisms of the sort() method, specific code examples are required. Introduction: Array sorting is one of the very common operations in our daily front-end development work. The array sorting method sort() in JavaScript is one of the most commonly used array sorting methods. However, do you really understand the principles and mechanisms of the sort() method? This article will give you an in-depth understanding of the principles and mechanisms of JS array sorting, and provide specific code examples. 1. Basic usage of sort() method

Guide to writing a custom sorting algorithm for PHP arrays Guide to writing a custom sorting algorithm for PHP arrays Apr 27, 2024 pm 06:12 PM

How to write a custom PHP array sorting algorithm? Bubble sort: Sorts an array by comparing and exchanging adjacent elements. Selection sort: Select the smallest or largest element each time and swap it with the current position. Insertion sort: Insert elements one by one into the sorted part.

How to keep key names after sorting PHP array by value? How to keep key names after sorting PHP array by value? May 02, 2024 pm 04:09 PM

The way to sort an array by value in PHP while preserving the key names is to use the usort() function to sort the array by value. Pass an anonymous function to the usort() function as a comparison function, which returns the difference in element values. usort() will sort the array according to the anonymous function while keeping the key names unchanged.

Sort arrays according to custom sorting rules in PHP, retaining original key names Sort arrays according to custom sorting rules in PHP, retaining original key names May 04, 2024 am 09:27 AM

In PHP, use the uasort() function to sort an array according to custom sorting rules while retaining the original key names. A custom comparison function is a function that takes two elements as input and returns an integer: a negative number means the former is less than the latter, zero means they are equal, and a positive number means the former is greater than the latter.

asort() function in PHP sorts array by value asort() function in PHP sorts array by value Nov 18, 2023 am 10:56 AM

The asort() function in PHP sorts an array by value. Specific code examples are required. PHP is a widely used server-side scripting language that has rich array processing functions. Among them, the asort() function is a very useful function, which can sort the array according to its value. This article will introduce the use of the asort() function in detail and give specific code examples. The function of the asort() function is to sort the array in ascending order by value while maintaining the association between keys and values. It is done by modifying the original number

Use PHP function 'sort' to sort an array in ascending order Use PHP function 'sort' to sort an array in ascending order Jul 25, 2023 am 09:28 AM

Sort an array in ascending order using PHP function "sort" In PHP, you can easily sort an array using built-in functions. Among them, the sort function is one of the most commonly used functions, which can sort an array in ascending order. This article will introduce how to use the sort function and give corresponding code examples. The syntax of the sort function is as follows: sort(array&$array,int$sort_flags=SORT_REGULAR):boo

Tips for multi-dimensional sorting of PHP arrays: Optimizing sorting efficiency Tips for multi-dimensional sorting of PHP arrays: Optimizing sorting efficiency Apr 30, 2024 am 09:54 AM

Tips for optimizing multi-dimensional array sorting in PHP: Create user-defined functions for sorting Use the array_multisort() function to apply multi-dimensional key reordering Practical case: Sorting products by array key-value pairs

See all articles