Sort a two-dimensional array in PHP.

In PHP, you can use the array_multisort() function to sort a two-dimensional array. This function sorts the array based on the specified columns.

Here is an example of sorting a two-dimensional array using the array_multisort() function.

// 假设有一个二维数组
$people = array(
    array('name' => 'John', 'age' => 20),
    array('name' => 'Jane', 'age' => 25),
    array('name' => 'Bob', 'age' => 18)
);

// 根据年龄升序排序
$ages = array_column($people, 'age'); // 获取年龄列
array_multisort($ages, SORT_ASC, $people); // 根据年龄排序

// 输出排序后的结果
foreach ($people as $person) {
    echo $person['name'] . ', ' . $person['age'] . '<br>';
}

The output result is:

Bob, 18
John, 20
Jane, 25

In the example above, the array_column() function is first used to retrieve the age column from the two-dimensional array, and then the array_multisort() function is used to sort the two-dimensional array in ascending order based on age. Finally, the sorted results are output using a foreach loop.

广告
Closing in 10 seconds
bannerAds