更改多维数组的键

时间:2013-11-18 04:15:40

标签: php arrays sorting multidimensional-array

我有一个我已解码的json数组,如下所示:

[d] =>
    [Current Markets] => [0] => [MarketNumber] => 54321
                             => [RunnerNumber] => 8
                             => [RaceNumber] => 02
                             => [StartingPrice] => $2
                             => [GameName] => something
                      => [1] => [MarketNumber] => 54321
                             => [RunnerNumber] => 2
                             => [RaceNumber] => 01
                             => [StartingPrice] => $1.5
                             => [GameName] => something
                      => [2] => [MarketNumber] => 54321
                             => [RunnerNumber] => 9
                             => [RaceNumber] => 03 
                             => [StartingPrice] => $9
                             => [GameName] => something
                      => [3] => [MarketNumber] => 54321
                             => [RunnerNumber] => 1
                             => [RaceNumber] => 03
                             => [StartingPrice] => $3.5
                             => [GameName] => something
                      => [4] => [MarketNumber] => 54321
                             => [RunnerNumber] => 4
                             => [RaceNumber] => 02
                             => [StartingPrice] => $2.25
                             => [GameName] => something
                      => [5] => [MarketNumber] => 54321
                             => [RunnerNumber] => 4
                             => [RaceNumber] => 01
                             => [StartingPrice] => $1.25
                             => [GameName] => something

现在,我不知道有多少场比赛,或者每场比赛可能有多少比赛,但是我需要能够通过MarketNumber然后比赛号码对比赛进行排序,然后按比赛中的跑垒员数量进行排序。

例如:

(MarketNumber)54321 => (RaceNumber)01 => (RunnerNumber)2 => [MarketNumber] => 54321
                                                         => [RunnerNumber] => 2
                                                         => [RaceNumber] => 01
                                                         => [StartingPrice] => $1.5
                                                         => [GameName] => something
                                      => (RunnerNumber)4 => [MarketNumber] => 54321
                                                         => [RunnerNumber] => 4
                                                         => [RaceNumber] => 01
                                                         => [StartingPrice] => $1.25
                                                         => [GameName] => something
                       (RaceNumber)02 => (RunnerNumber)4 => [MarketNumber] => 54321
                                                         => [RunnerNumber] => 4
                                                         => [RaceNumber] => 02
                                                         => [StartingPrice] => $2.25
                                                         => [GameName] => something
                                      => (RunnerNumber)8 => [MarketNumber] => 54321
                                                         => [RunnerNumber] => 8
                                                         => [RaceNumber] => 02
                                                         => [StartingPrice] => $2
                                                         => [GameName] => something
                       (RaceNumber)03 => (RunnerNumber)1 => [MarketNumber] => 54321
                                                         => [RunnerNumber] => 1
                                                         => [RaceNumber] => 03
                                                         => [StartingPrice] => $3.5
                                                         => [GameName] => something
                                      => (RunnerNumber)1 => [MarketNumber] => 54321
                                                         => [RunnerNumber] => 9
                                                         => [RaceNumber] => 03 
                                                         => [StartingPrice] => $9
                                                         => [GameName] => something

我已经尝试过array_merge_recursive,但它似乎没有做任何事情,我尝试过ksort()和asort(),但它没有正确排序,即使是使用标志。

现在,我在:

foreach($array as $value) {
            $end = array($array['DisplayNumber'] => $array);
            $list = array($array => $end);
            print_r($list);
        }

虽然它的分类非常好,但我似乎无法让它拥有一个键,然后是数组等:

[MarketNumber] => [RaceNumber01] => [RunnerNumber1] => OtherStuff
                                 => [RunnerNumber2] => OtherStuff
                                 => [RunnerNumber3] => OtherStuff
               => [RaceNumber02] => [RunnerNumber1] => OtherStuff
                                 => [RunnerNumber2] => OtherStuff
                                 => [RunnerNumber3] => OtherStuff
               => [RaceNumber03] => [RunnerNumber1] => OtherStuff
                                 => [RunnerNumber2] => OtherStuff
                                 => [RunnerNumber3] => OtherStuff

我认为这可能是因为它是一个foreach循环,但我无法弄清楚如何改变它,因为它只显示最后一次迭代。

1 个答案:

答案 0 :(得分:1)

没有一个神奇的功能可以为你做到这一点,但是构建你自己的循环来处理它是非常简单的。

基本上,我们只是循环输入,并将每个项目插入结果数组中的适当位置,根据需要创建任何中间数组。

这样的事情:

$output = array();
foreach ($input['d']['Current Markets'] as $item) {
    $market = $item['MarketNumber'];
    $race = $item['RaceNumber'];
    $runner = $item['RunnerNumber'];
    if (!isset($output[$market])) $output[$market] = array();
    if (!isset($output[$market][$race])) $output[$market][$race]= array();
    $output[$market][$race][$runner] = $item;
}

我不确定您是否只想对上述代码处理的数据进行分组,或者实际对其进行排序。如果你想对它进行排序,我们只需要在最后遍历每个组并使用ksort(),如下所示:

foreach ($output as $market => $marketGroup) {
    foreach ($output[$market] as $race => $raceGroup) {
        ksort($output[$market][$race]); // sort race group by runner
    }
    ksort($output[$market]); // sort market group by race
}
ksort($output); // sort overall data by market