0

我有一个数组。它看起来像这样:

[1=Name][2=Type][3=Company][4=State][5=Date]

[6=Name][7=Type][8=Company][9=State][10=Date]

[11=Name][12=Type][13=Company][14=State][15=Date]

现在我想按名称按 AZ 顺序对每 5 个值进行排序。因此[1=Name][2=Type][3=Company][4=State][5=Date]将被视为需要排序的一行,[6=Name][7=Type][8=Company][9=State][10=Date]并将被视为第二行。他们需要按他们的名字排序,[1]& [6]...

我从来没有遇到过这种情况,因此我努力寻找解决方案。

有任何想法吗?

4

1 回答 1

0

问题不是很清楚,请提供一些预期输出的清晰示例。无论我理解什么,您都可以尝试这样的代码:

$arr=array("1" => "name1", "2" => "game4", "3" => "game10", "4" => "game2",
   "5" => "game3", "6" => "name4", "7" => "game4", "8" => "game10",
   "9" => "game2", "10" => "game3", "11" => "name3", "12" => "game4",
   "13" => "game10", "14" => "game2", "15" => "game3");
$narr = array();
for ($i=0; $i<count($arr)/5; $i++)
   $narr[] = array_slice($arr, $i * 5, 5, true);
function cmp($a, $b) {
    return strcmp(reset($a), reset($b));
}
uasort($narr, 'cmp');
print_r($narr);

输出:

Array
(
    [0] => Array
        (
            [1] => name1
            [2] => game4
            [3] => game10
            [4] => game2
            [5] => game3
        )
    [2] => Array
        (
            [11] => name3
            [12] => game4
            [13] => game10
            [14] => game2
            [15] => game3
        )
    [1] => Array
        (
            [6] => name4
            [7] => game4
            [8] => game10
            [9] => game2
            [10] => game3
        )
)
于 2013-02-19T12:22:30.503 回答