Foreach用于嵌套数组

时间:2015-12-08 18:43:52

标签: php arrays

我有这个数组

Array
(
    [0] => Array
        (
            [PlayerID] => 0
            [Nickname] => nickname1
            [Score] => 291
            [Ping] => 204
        )

    [1] => Array
        (
            [PlayerID] => 1
            [Nickname] => nickname2
            [Score] => 4619
            [Ping] => 309
        )
)

我使用了这段代码:

    <?php

    $API_GET = file_get_contents("player_list.php"); 
    $API_GET = json_decode($API_GET); 
    $API = array(); 
 foreach($API_GET as $ID => $Info)
 {
    $Player[$ID] = $Info;
    foreach($Info as $key => $value)
   { 
        $API[$key] = $value; 
        echo $value . "<br/>";
    }
  }

 ?>

我收到警告“为foreach()提供的无效参数”,我不确定是什么导致了这个,因为我是PHP的新手。

〜斯坦利

1 个答案:

答案 0 :(得分:1)

您已提前关闭括号。看看:

 $API_GET = file_get_contents("player_list.php"); 
 $API_GET = json_decode($API_GET);
 $API = array(); 
 foreach($API_GET as $ID => $Info)
 {
    $Player[$ID] = $Info;
    foreach($Info as $key => $value)
   { 
        $API[$key] = $value; 
        echo $value . "<br/>";
    }
  }