我想我应该重写它,以便使它更加清晰,并有望消除混乱。
莱斯说我有好几层楼的办公室。我想把那些办公室按每层楼的最低编号排序所以结果看起来像这样:
-------------------------
| floor | office | sort |
-------------------------
| 1 | 4 | 1 |
| 2 | 4 | 2 |
| 3 | 3 | 3 |
| 4 | 3 | 4 |
| 1 | 5 | 5 |
| 2 | 5 | 6 |
| 3 | 4 | 7 |
| 4 | 4 | 8 |
| 1 | 6 | 9 |
| 2 | 6 | 10 |
| 3 | 5 | 11 |
| 4 | 5 | 12 |
| 2 | 7 | 13 |
| 3 | 6 | 14 |
| 4 | 6 | 15 |
| 3 | 7 | 16 |
| 4 | 7 | 17 |
-------------------------
阵列:
array (
0 => array (
'floor' => 1,
'office' => 4,
),
1 => array (
'floor' => 1,
'office' => 5,
),
2 => array (
'floor' => 1,
'office' => 6,
),
3 => array (
'floor' => 2,
'office' => 4,
),
4 => array (
'floor' => 2,
'office' => 5,
),
5 => array (
'floor' => 2,
'office' => 6,
),
6 => array (
'floor' => 2,
'office' => 7,
),
7 => array (
'floor' => 3,
'office' => 3,
),
8 => array (
'floor' => 3,
'office' => 4,
),
9 => array (
'floor' => 3,
'office' => 5,
),
10 => array (
'floor' => 3,
'office' => 6,
),
11 => array (
'floor' => 3,
'office' => 7,
),
12 => array (
'floor' => 4,
'office' => 3,
),
13 => array (
'floor' => 4,
'office' => 4,
),
14 => array (
'floor' => 4,
'office' => 5,
),
15 => array (
'floor' => 4,
'office' => 6,
),
16 => array (
'floor' => 4,
'office' => 7,
)
, )
我需要做的是一次一层地穿过每一层,进入编号最低的办公室,然后从编号最低的楼层开始,直到筋疲力尽。
您可能想要的是usort
函数。类似的东西
function racerCompare($a, $b) {
if ($a['heat_nbr'] > $b['heat_nbr'}) {
return -1;
} else if ($a['heat_nbr'] < $b['heat_nbr'}) {
return 1;
} else {
return 0;
}
}
usort($startingArray, 'racerCompare');
racerCompare
逻辑可能有点不同(这个问题还不完全清楚)。