我想我应该重写这段代码,以便让它更清晰一点,并有望消除混乱。
Les说我有好几层办公室。我想按每个楼层的最低编号对这些办公室进行排序,结果如下所示:
-------------------------
| 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,
)
, )我需要做的是一次循环遍历每一层,选择编号最低的办公室,然后从编号最低的楼层重新开始,直到筋疲力尽。
发布于 2016-01-07 11:31:47
您可能需要的是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的逻辑可能有一点不同(从问题中看并不完全清楚)。
https://stackoverflow.com/questions/34646809
复制相似问题