0

我有这个数组:

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 做同样的事情。我该怎么做?

4

2 回答 2

1

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

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

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

于 2013-03-05T20:14:37.633 回答
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
)
于 2013-03-06T05:22:40.180 回答