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

时间:2022-04-24 12:14:07

I'm working on a custom Single elimination bracket which has to automatically check for results and make a create a round based on results. I can successfully check the results and save the teams in array for the next round but my problem is that it creates a strange array:

我正在使用一个定制的单消除括号,它必须自动检查结果,并根据结果创建一个回合。我可以成功地检查结果并保存下一轮的数组,但我的问题是它创建了一个奇怪的数组:

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

OUTPUT:

输出:

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

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

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

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

    )

What I need is to reindex this array and make array_chunk($teams, 2) so I can then save the information to a database and make this process again until the whole tournament is covered.

我需要的是重新索引这个数组并生成array_chunk($team, 2),这样我就可以将信息保存到数据库中,并在整个锦标赛被覆盖之前再进行这个过程。

Output that I want:

我想要的输出:

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

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

            )

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

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

    )

How can I do that?

我该怎么做呢?

2 个解决方案

#1


0  

I believe that what you are trying to do is something like this:

我相信你想做的事情是这样的:

$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);

I omitted the inner conditionals, because they aren't relevant to the answer, but you'll have to do what you have to do to fill in that inner info.

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

#2


0  

(Posted solution on behalf of the OP).

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

I played a little bit more with Octopus's answer (look below on comments) and tweaked the code so it works perfectly. The example in Octopus' answer showed one index too many so I came up with this:

我用了八达通的答案(看下面的评论),稍微做了一些改动,然后修改了代码,使其工作完美。章鱼的答案显示了一个指数太多,所以我想到了这个:

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

Explanation:

解释:

I just got rid of array declaration and added array_chunk to it so it's seen as a match.

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

Output:

输出:

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

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

        )

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

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

            )

    )

#1


0  

I believe that what you are trying to do is something like this:

我相信你想做的事情是这样的:

$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);

I omitted the inner conditionals, because they aren't relevant to the answer, but you'll have to do what you have to do to fill in that inner info.

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

#2


0  

(Posted solution on behalf of the OP).

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

I played a little bit more with Octopus's answer (look below on comments) and tweaked the code so it works perfectly. The example in Octopus' answer showed one index too many so I came up with this:

我用了八达通的答案(看下面的评论),稍微做了一些改动,然后修改了代码,使其工作完美。章鱼的答案显示了一个指数太多,所以我想到了这个:

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

Explanation:

解释:

I just got rid of array declaration and added array_chunk to it so it's seen as a match.

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

Output:

输出:

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

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

        )

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

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

            )

    )