PHP如何合并数组并创建密钥?

时间:2016-09-14 15:12:08

标签: php arrays

我正在开发一个自定义单消除支架,它必须自动检查结果并根据结果创建一个圆形。我可以成功检查结果并将数组保存在数组中以进行下一轮但我的问题是它创建了一个奇怪的数组:

    foreach ($competitors as $competitor) {
                    $teams = array();

                    if ($competitor['scoreHome'] > $competitor['scoreAway']) {
                        $teams = array(array('home'=>$competitor['home']));
                    } 

                    if ($competitor['scoreHome'] < $competitor['scoreAway'] ) {
                        $teams = array(array('away'=>$competitor['away']));
                    }
    }               
                    print_r($teams);

输出:

    Array
    (
        [0] => Array
        (
            [away] => testis5
        )

    )
    Array
    (
        [0] => Array
        (
            [home] => wdefgr
        )

    )
    Array
    (
        [0] => Array
        (
            [away] => testis4
        )

    )
    Array
    (
        [0] => Array
        (
            [home] => team1
        )

    )

我需要的是重新索引这个数组并制作array_chunk($teams, 2),这样我就可以将信息保存到数据库中并再次进行此过程直到整个锦标赛都被覆盖。

我想要的输出:

    Array
    (
    [0] => Array
        (
            [0] => Array
                (
                    [teamName] => testis1
                )

            [1] => Array
                (
                    [teamName] => testis5
                )

            )

    [1] => Array
    (
        [0] => Array
            (
                [teamName] => wdefgr
            )

        [1] => Array
            (
                [teamName] => testis2
            )

    )

我该怎么做?

2 个答案:

答案 0 :(得分:0)

我相信你要做的就是这样:

$teams = array();
foreach ($competitors as $competitor) {

  $match = array();
  /* fill this match with the info you want, eg. */
  $match[] = array(array('home'=>$competitor['home']));
  $match[] = array(array('away'=>$competitor['away']));

  $team[] = $match;
}               
print_r($teams);

我省略了内部条件,因为它们与答案无关,但你必须做你必须做的事情才能填写内部信息。

答案 1 :(得分:0)

(代表OP发布解决方案)。

我用Octopus的答案(看下面的评论)进行了一些调整,并调整了代码以使其完美运行。八达通的例子&#39;答案显示一个指数太多,所以我提出了这个:

    foreach ($competitors as $competitor) {

        if ($competitor['scoreHome'] > $competitor['scoreAway']) {
            $match[] = array('home'=>$competitor['home']);
        } 

        if ($competitor['scoreHome'] < $competitor['scoreAway'] ) {
            $match[] = array('away'=>$competitor['away']);
        }
    }

    $matches = array_chunk($match, 2);
    print_r($matches);

说明:

我刚刚摆脱了数组声明并添加了array_chunk,因此它被视为匹配。

输出:

    Array
    (
        [0] => Array
            (
                [0] => Array
                    (
                        [away] => testis5
                    )

                [1] => Array
                    (
                        [home] => wdefgr
                    )

        )

        [1] => Array
            (
                [0] => Array
                    (
                        [away] => testis4
                    )

                [1] => Array
                    (
                        [home] => team1
                    )

            )

    )