数组PHP数组排序数组、PHP

2023-09-11 23:25:56 作者:太可笑的我

我有folloring阵列结构:

I have the folloring array structure:

$list = array();
$element1 = array('start' => '10', 'end' => '15');
$element2 = array('start' => '1',  'end' => '5');
$list[] = $element1;
$list[] = $element2;

的每一个元素开始结束是数字而已。

我要排序 $列表启动值。我怎样才能做到这一点用途不同?

I would like to sort $list by start values. How can I do that effectivly?

推荐答案

您可以使用 usort 与此相比功能:

You can use usort with this comparison function:

function cmp($a, $b) {
    if ($a['start'] == $b['start']) {
        return $a['end'] - $b['end'];
    } else {
        return $a['start'] - $b['start'];
    }
}

通过这种比较功能的元素被下令将他们的启动的值,然后再由他们的结束的值。

With this comparison function the elements are ordered by their start value first and then by their end value.