如何识别具有值的数组键

时间:2013-08-19 09:04:33

标签: php mysql arrays

我有一张如下表:
enter image description here

并使用以下方式将结果计算为数组

while($Br_ROWS = mysql_fetch_assoc($Bracket_Query)){
     $Bracket_Result[] = $Br_ROWS;
}

如下所示的数组结果:

Array
(
    [0] => Array
        (
            [Guid] => 1
            [Tournament] => 1
            [Position] => A1-1
            [Player] => 123
            [Team] => 
        )

    [1] => Array
        (
            [Guid] => 2
            [Tournament] => 1
            [Position] => A2-1
            [Player] => 325
            [Team] => 
        )

    [2] => Array
        (
            [Guid] => 3
            [Tournament] => 1
            [Position] => B2-1
            [Player] => 325
            [Team] => 
        )

    [3] => Array
        (
            [Guid] => 4
            [Tournament] => 1
            [Position] => A1-2
            [Player] => 235
            [Team] => 
        )

)

我尝试但不知道该怎么做。
如果位置为A2-1(例如),如何指定播放器的值 我使用的是错误的方式?

1 个答案:

答案 0 :(得分:1)

只需foreach

foreach ($Bracket_Result as $row)
{
   if ($row['Position'] == "A1-2") {
      echo "I found you:".$row['Player'];
   }
}

或者你可以创建一个返回玩家的函数:

function getPlayerbyPosition($players, $position)
{
    foreach ($players as $player)
    {
       if ($player['Position'] == $position) {
          return $player;
       }
    }
}

$result = getPlayerbyPosition($Bracket_Result, "A1-2");
var_dump($result);