在PHP中,当您知道另一个值时获取数组值

时间:2014-10-01 09:56:08

标签: php arrays multidimensional-array

我找不到任何解决这个问题的办法,我的所有尝试都失败了,所以我需要你的帮助。

我必须定义在足球锦标赛中将要面对面的不同球队。我知道锦标赛的4组球队以及每天应该参加哪些比赛,但是由于这些信息,我无法显示锦标赛计划。

我有2个阵列:

- 锦标赛计划(由每个小组中每个小组的排名定义,每个小组每天有2场比赛):

$tournament = array(
    "day1" => array(1=> array("team1", "team2"), 2=> array("team3", "team4")),
    "day2" => array(1=> array("team3", "team1"), 2=> array("team4", "team2")),
    "day3" => array(1=> array("team1", "team4"), 2=> array("team2", "team3")),
    "day4" => array(1=> array("team2", "team1"), 2=> array("team4", "team3")),
    "day5" => array(1=> array("team1", "team3"), 2=> array("team2", "team4")),
    "day6" => array(1=> array("team4", "team1"), 2=> array("team3", "team2"))
);

- 小组详情:

$users_group = array ( 
    [0] => Array ( [team] => Superteam1 [rank] => 1 [group] => A ) 
    [1] => Array ( [team] => Superteam2 [rank] => 2 [group] => A ) 
    [2] => Array ( [team] => Superteam3 [rank] => 3 [group] => A ) 
    [3] => Array ( [team] => Superteam4 [rank] => 4 [group] => A ) 
    [4] => Array ( [team] => Superteam5 [rank] => 1 [group] => B ) 
    [5] => Array ( [team] => Superteam6 [rank] => 2 [group] => B ) 
    [6] => Array ( [team] => Superteam7 [rank] => 3 [group] => B ) 
    [7] => Array ( [team] => Superteam8 [rank] => 4 [group] => B ) 
    [8] => Array ( [team] => Superteam9 [rank] => 1 [group] => C ) 
    [9] => Array ( [team] => Superteam10 [rank] => 2 [group] => C ) 
    [10] => Array ( [team] => Superteam11 [rank] => 3 [group] => C ) 
    [11] => Array ( [team] => Superteam12 [rank] => 4 [group] => C ) 
    [12] => Array ( [team] => Superteam13 [rank] => 1 [group] => D ) 
    [13] => Array ( [team] => Superteam14 [rank] => 2 [group] => D ) 
    [14] => Array ( [team] => Superteam15 [rank] => 3 [group] => D ) 
    [15] => Array ( [team] => Superteam16 [rank] => 4 [group] => D ) 
) 

我希望,在比赛的每一天,显示将要进行的两场比赛。然而我尝试的东西不起作用,我只能得到第一场比赛的第一支球队(N.B.第一次使用循环来从我的数据库中获得6天的锦标赛):

$i=1;
while($i<7){
    echo '<div>Day '.$i.'</div>';

    $max = count( $users_group ); 
    for( $l = 0; $l < $max; $l++ ){
        if($users_group[$l]['rang']==substr($tournament["day".$i][1][0],-1)){
          echo $users_group[$l]['team']; 
        }
        if($users_group[$l]['rang']==substr($tournament["day".$i][1][1],-1)){
          echo ' VS '.$users_group[$l]['team'] . '<br />'; 
        }

        if($users_group[$l]['rang']==substr($tournament["day".$i][2][0],-1)){
          echo $users_group[$l]['team']; 
        }
        if($users_group[$l]['rang']==substr($tournament["day".$i][2][1],-1)){
          echo ' VS '.$users_group[$l]['team'] . '<br />'; 
        }


    }
    $i++;
}

我想表现出类似的东西:

Day 1

Superteam1 VS Superteam2
Superteam3 VS Superteam4

Superteam5 VS Superteam6
Superteam7 VS Superteam8

Superteam9 VS Superteam10
Superteam11 VS Superteam12

Superteam13 VS Superteam14
Superteam15 VS Superteam16


Day 2

Superteam3 VS Superteam1
Superteam4 VS SUperteam2

etc
.
.
.

我尝试创建一个函数,以避免每次只获取$users_group[$l]['team']的第一个值,但我无法使其正常工作。

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

以下代码将完成这项工作。我创建了新的数据结构,以便更轻松地访问有关组,团队,日期等的数据。我在代码中添加了注释和解释,但请问您是否不理解它在做什么。

<?php

$tournament = array(
    "day1" => array(1=> array("team1", "team2"), 2=> array("team3", "team4")),
    "day2" => array(1=> array("team3", "team1"), 2=> array("team4", "team2")),
    "day3" => array(1=> array("team1", "team4"), 2=> array("team2", "team3")),
    "day4" => array(1=> array("team2", "team1"), 2=> array("team4", "team3")),
    "day5" => array(1=> array("team1", "team3"), 2=> array("team2", "team4")),
    "day6" => array(1=> array("team4", "team1"), 2=> array("team3", "team2"))
);


$users_group = array (
    array( 'team' => 'Superteam1', 'rank' => 1, 'group' => 'A' ),
    array( 'team' => 'Superteam2', 'rank' => 2, 'group' => 'A' ),
    array( 'team' => 'Superteam3', 'rank' => 3, 'group' => 'A' ),
    array( 'team' => 'Superteam4', 'rank' => 4, 'group' => 'A' ),
    array( 'team' => 'Superteam5', 'rank' => 1, 'group' => 'B' ),
    array( 'team' => 'Superteam6', 'rank' => 2, 'group' => 'B' ),
    array( 'team' => 'Superteam7', 'rank' => 3, 'group' => 'B' ),
    array( 'team' => 'Superteam8', 'rank' => 4, 'group' => 'B' ),
    array( 'team' => 'Superteam9', 'rank' => 1, 'group' => 'C' ),
    array( 'team' => 'Superteam10', 'rank' => 2, 'group' => 'C' ),
    array( 'team' => 'Superteam11', 'rank' => 3, 'group' => 'C' ),
    array( 'team' => 'Superteam12', 'rank' => 4, 'group' => 'C' ),
    array( 'team' => 'Superteam13', 'rank' => 1, 'group' => 'D' ),
    array( 'team' => 'Superteam14', 'rank' => 2, 'group' => 'D' ),
    array( 'team' => 'Superteam15', 'rank' => 3, 'group' => 'D' ),
    array( 'team' => 'Superteam16', 'rank' => 4, 'group' => 'D' ),
);

# this function is used by array_map to remove "team" from the $tournament data struct.
$func = function($value) {
    $value = str_replace("team", "", $value);
    return $value;
};

# redo the tournament array to remove all the "day" and "team" strings
foreach ($tournament as $day => $val) {
    $new_day = str_replace("day", "", $day);
    foreach ($val as $m => $t) {
        # this removes "team" from the values
        $days[$new_day][$m] = array_map( $func, $t );
    }
}

# turn the users_group array into a multidimension array in the form
# $ranked_ug = [ group ][ rank ] = team
$ranked_ug = array();
foreach( $users_group as $u => $data ){
    $ranked_ug[ $data['group'] ][ $data['rank'] ] = $data['team'];
}

# this is used to get data about the team from $ranked_ug
function getKey( $t_data ) {
    return function ( $value ) use ($t_data) {
        return $t_data[$value];
    };
}

# for each day...
foreach ($days as $d => $d_data) {
    echo "<h2>Day $d</h2>\n";
    # iterate through each group
    foreach ($ranked_ug as $g => $g_data) {
        echo "<h3>Group $g Matches</h3>\n";
        # look at each match
        foreach ($d_data as $match_no => $teams) {
            echo "<p>Match $match_no<br>\n";
            # $teams contains the numbers of the teams playing
            # pull the team name from $ranked_ug using $ranked_ug[$g][ rank ]
            # this line uses implode to join the team names with "vs"
            # and array_map to automatically get the team name from $ranked_ug
            echo implode(" vs ",
                array_map( getKey( $ranked_ug[$g] ), $teams ) ) . "</p>\n";
        }
        echo "\n";
    }
    echo "\n";
}

前三天的A组和B组输出:

<h2>Day 1</h2>
<h3>Group A Matches</h3>
<p>Match 1<br>
Superteam1 vs Superteam2</p>
<p>Match 2<br>
Superteam3 vs Superteam4</p>

<h3>Group B Matches</h3>
<p>Match 1<br>
Superteam5 vs Superteam6</p>
<p>Match 2<br>
Superteam7 vs Superteam8</p>


<h2>Day 2</h2>
<h3>Group A Matches</h3>
<p>Match 1<br>
Superteam3 vs Superteam1</p>
<p>Match 2<br>
Superteam4 vs Superteam2</p>

<h3>Group B Matches</h3>
<p>Match 1<br>
Superteam7 vs Superteam5</p>
<p>Match 2<br>
Superteam8 vs Superteam6</p>


<h2>Day 3</h2>
<h3>Group A Matches</h3>
<p>Match 1<br>
Superteam1 vs Superteam4</p>
<p>Match 2<br>
Superteam2 vs Superteam3</p>

<h3>Group B Matches</h3>
<p>Match 1<br>
Superteam5 vs Superteam8</p>
<p>Match 2<br>
Superteam6 vs Superteam7</p>