数组在多个元素

时间:2016-01-07 03:18:00

标签: php arrays loops sorting indexing

我认为我会重写这一点,以使其更清晰,并希望消除混乱。

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, 
            )
, )

我需要做的是一次一个地环绕每个楼层,然后从最低编号的办公室开始,然后再从最低编号的楼层开始,直到筋疲力尽。

1 个答案:

答案 0 :(得分:1)

您可能想要的是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逻辑或许有点不同(问题并不完全清楚)。