按字母顺序使用一个子数组值PHP数组排序 [英] PHP sort array alphabetically using a subarray value
本文介绍了按字母顺序使用一个子数组值PHP数组排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:结果
我怎么排序在PHP <多维数组BR>
PHP排序数组通过子数组值结果
PHP排序多维数组由值
我的数组是这样的:
Array(
[0] => Array(
[name] => Bill
[age] => 15
),
[1] => Array(
[name] => Nina
[age] => 21
),
[2] => Array(
[name] => Peter
[age] => 17
)
);
我想他们根据他们的名字字母排序。
我看到 PHP排序数组通过子数组值,但它并没有太大的帮助。
任何想法如何做到这一点?
I would like to sort them in alphabetic order based on their name. I saw PHP Sort Array By SubArray Value but it didn't help much. Any ideas how to do this?
推荐答案
下面是你的答案和它的作品100%,我测试过了。
Here is your answer and it works 100%, I've tested it.
<?php
$a = Array(
1 => Array(
'name' => 'Peter',
'age' => 17
),
0 => Array(
'name' => 'Nina',
'age' => 21
),
2 => Array(
'name' => 'Bill',
'age' => 15
),
);
function compareByName($a, $b) {
return strcmp($a["name"], $b["name"]);
}
usort($a, 'compareByName');
/* The next line is used for debugging, comment or delete it after testing */
print_r($a);
这篇关于按字母顺序使用一个子数组值PHP数组排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文