在条件表中显示数组(PHP)

时间:2013-03-05 20:12:44

标签: php arrays

我有这个数组:

Array ( [2-3] => player1 [1-3] => player2 [2-0] => player1 [5-1] => player1 [2-4] => player2 [4-1] => player2 )

我想要的是:if array value is "player1" show all its keys in a table

player1的结果应该是表格中的结果:

  • 2-3
  • 2-0
  • 5-1

我会让他们对另一张桌子中的player2做同样的事情。我该怎么办?

2 个答案:

答案 0 :(得分:1)

您可以使用简单的循环来执行此操作:

$target = 'player1';
$result = array();
foreach ($array as $values => $player) {
    if ($player === $target) {
        $result[] = $values;
    }
}

您可以为其他玩家更改$target

答案 1 :(得分:0)

试试这个:

$array   = Array ( "2-3" => "player1", "1-3" => "player2", "2-0" => "player1", "5-1" => "player1", "2-4" => "player2", "4-1" => "player2" );

$res     = array();
for($i=0;$i<count($array);$i++){
   $key     = array_search("player1",$array);
   if($key){
       $res[]   = $key;
       $array[$key]  = "";
   }
}
echo "<pre>";
print_r($res);

输出:

Array
(
    [0] => 2-3
    [1] => 2-0
    [2] => 5-1
)