我正在尝试对这个多维数组进行排序。我不想按照第二个数组中包含的名称对数组的第一个维度进行排序。
我如何按“字母”字母顺序排序:
Array
(
[0] => Array
(
["name"] => "Delta"
["other1"] => "other data..."
["other2"] => "other data..."
)
[1] => Array
(
["name"] => "Beta"
["other1"] => "other data..."
["other2"] => "other data..."
)
[2] => Array
(
["name"] => "Alpha"
["other1"] => "other data..."
["other2"] => "other data..."
)
)
所以它就这样结束了:
Array
(
[0] => Array
(
["name"] => "Alpha"
["other1"] => "other data..."
["other2"] => "other data..."
)
[1] => Array
(
["name"] => "Beta"
["other1"] => "other data..."
["other2"] => "other data..."
)
[2] => Array
(
["name"] => "Delta"
["other1"] => "other data..."
["other2"] => "other data..."
)
)
非常感谢一些帮助!
答案 0 :(得分:0)
<?php
$array = array(
array(
"name" => "Delta",
"other1" => "other data...",
"other2" => "other data...",
),
array(
"name" => "Beta",
"other1" => "other data...",
"other2" => "other data...",
),
array(
"name" => "Alpha",
"other1" => "other data...",
"other2" => "other data...",
),
);
usort($array, function($a, $b) {
return strcmp($a['name'], $b['name']);
});
print_r($array);
输出:
Array
(
[0] => Array
(
[name] => Alpha
[other1] => other data...
[other2] => other data...
)
[1] => Array
(
[name] => Beta
[other1] => other data...
[other2] => other data...
)
[2] => Array
(
[name] => Delta
[other1] => other data...
[other2] => other data...
)
)