在子数组中按值排序数组

时间:2014-04-09 09:58:16

标签: php arrays sorting key

我有一个这样的数组:

[
   ['translationID'] => 1,
   ['locale'] => 'nl_BE',
   ['translation'] => 'U bent aangemeld'
]
[
   ['translationID'] => 2,
   ['locale'] => 'de_DE',
   ['translation'] => 'Sie sind angemeldet als'
]

现在我想要一个包含所有数组的数组,其中键locale = de_DE 首先!这可能吗?

1 个答案:

答案 0 :(得分:1)

试试这个,

$arr = array(
        array("translationID" => "1","locale" => "nl_BE","translation" => "U bent aangemeld"),
        array("translationID" => "2","locale" => "de_DE","translation" => "Sie sind angemeldet als")
        );

function cmp($a, $b) {
    if ($a["locale"] == $b["locale"]) {
            return 0;
    }
    return ($a["locale"] < $b["locale"]) ? -1 : 1;
}
usort($arr,"cmp");