# PHP如何对二维数组(多维数组)进行排序

``````// 假设有一个二维数组 \$data
\$data = array(
array('name' => 'Alice', 'age' => 28),
array('name' => 'Bob', 'age' => 22),
array('name' => 'Charlie', 'age' => 25),
);

// 提取出要排序的键的值，放入临时数组中
\$ages = array_column(\$data, 'age');

// 使用 array_multisort() 对临时数组及原始数组进行排序
array_multisort(\$ages, SORT_ASC, \$data);

// 输出排序后的数组
print_r(\$data);``````

``````\$data = array(
array('name' => 'Alice', 'age' => 28),
array('name' => 'Bob', 'age' => 22),
array('name' => 'Charlie', 'age' => 25),
);

usort(\$data, function(\$a, \$b) {
return \$a['age'] - \$b['age'];
});

print_r(\$data);``````

``````\$data = array(
array('name' => 'Alice', 'age' => 28),
array('name' => 'Bob', 'age' => 22),
array('name' => 'Charlie', 'age' => 25),
);

\$ages = array_column(\$data, 'age');
\$names = array_column(\$data, 'name');

array_multisort(\$ages, SORT_ASC, \$names, SORT_ASC, \$data);

print_r(\$data);``````

``````\$data = array(
array('name' => 'Alice', 'age' => 28),
array('name' => 'Bob', 'age' => 22),
array('name' => 'Charlie', 'age' => 25),
);

\$data = array_map(function(\$item) {
return array_map('strtolower', \$item);
}, \$data);

array_multisort(array_column(\$data, 'age'), SORT_ASC, \$data);

print_r(\$data);``````

(=￣ω￣=)··· 暂无内容！

6

0

10

7