How to arrange two-dimensional array in php

PHPz
Release: 2023-04-26 09:37:32
Original
497 people have browsed it

PHP is a very excellent programming language that supports accessing and operating various types of arrays. Whether it is a one-dimensional array or a multi-dimensional array, it can be sorted and processed using various functions provided in PHP. This article will discuss in detail how to sort a two-dimensional array in PHP.

What is a two-dimensional array?

First of all, we need to know what a two-dimensional array is. A two-dimensional array refers to an array structure composed of multiple one-dimensional arrays, which can be understood as a table or matrix. For example, in a student information management system, each student's information forms a one-dimensional array, and all students' information forms a two-dimensional array.

How to create a two-dimensional array?

In PHP, we can create a two-dimensional array in a variety of ways, for example:

  1. Direct initialization

can be set in the array variable Directly initialize a two-dimensional array using key-value pairs:

$students = array(
    array('name' => '张三', 'age' => 18, 'score' => 80),
    array('name' => '李四', 'age' => 20, 'score' => 90),
    array('name' => '王五', 'age' => 19, 'score' => 85)
);
Copy after login
  1. Assign values ​​one by one

You can also add elements to the array one by one to create a two-dimensional array:

$students = array();
$students[] = array('name' => '张三', 'age' => 18, 'score' => 80);
$students[] = array('name' => '李四', 'age' => 20, 'score' => 90);
$students[] = array('name' => '王五', 'age' => 19, 'score' => 85);
Copy after login

How to sort a two-dimensional array?

After having a two-dimensional array, we often need to sort it in order to better present the data and display the results. In PHP, two-dimensional array sorting can be performed using built-in functions.

  1. Sort by specified key value

You can use the built-in sort function in PHP to sort the entire two-dimensional array by specifying the key name. For example, the following example can sort the entire array according to the key name "score":

function sortByScore($a, $b) {
    return $a['score'] < $b[&#39;score&#39;];
}

usort($students, &#39;sortByScore&#39;);
Copy after login

The sortByScore here is a callback function used to define the comparison method. The usort function is used to sort the array. The second parameter specifies the name of the comparison function.

  1. Sort by multiple key values

If you need to sort by multiple key values, you can sort multiple key values ​​by comparing them in the callback function . For example, the following example can be sorted by the "score" key first, and then sorted by the "age" key if each score is the same:

function sortByScoreAndAge($a, $b) {
    if ($a[&#39;score&#39;] == $b[&#39;score&#39;]) {
        return ($a[&#39;age&#39;] < $b[&#39;age&#39;]) ? -1 : 1;
    } else {
        return ($a[&#39;score&#39;] > $b['score']) ? -1 : 1;
    }
}

usort($students, 'sortByScoreAndAge');
Copy after login

The method here is to first sort by the "score" ” is sorted, and if two students have the same score, they are sorted by “age”.

  1. Sort by key value type

Sometimes, we need to sort the two-dimensional array according to the type of key value for better subsequent processing. Arrays can be sorted using the built-in array_multisort function in PHP. For example, the following example can sort the entire array by the type of the "score" key value:

$score = array_column($students, 'score');  // 获取所有学生的分数
array_multisort($score, SORT_ASC, $students);
Copy after login

The array_column function here is used to get the scores of all students and store them in the $score array. The array_multisort function is used to sort the entire array, and the sorted results will be stored in the $students variable.

Conclusion

The above is a discussion on sorting two-dimensional arrays in PHP. When we need to process a large amount of data, array sorting is often an indispensable part. Whether sorting by a single key value or multiple key values, PHP provides appropriate built-in functions to help us sort easily. We hope this article was helpful to you.

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

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