0

使用 PHP,我将如何从类似于此的 json 提要访问内部数组值,具体到此示例的每个游戏数组的值:

Array
(
[startIndex] => 3
[refreshInterval] => 60
[games] => Array
    (
        [0] => Array
            (
                [id] => 2013020004
                [gs] => 5
                [ts] => WEDNESDAY 10/2
                [tsc] => final
                [bs] => FINAL
                [bsc] => final
            )

        [1] => Array
            (
                [id] => 2013020005
                [gs] => 5
                [ts] => WEDNESDAY 10/2
                [tsc] => final
                [bs] => FINAL
                [bsc] =>
            )

我尝试在类似于这样的 foreach 循环中嵌套一个 foreach 循环:

foreach ($json as $key => $jsons) {
foreach ($jsons as $my => $value) {
    echo $value; 
}
}
4

4 回答 4

2

如果这是您正在查看的数组,那么您可以达到这些值

foreach($json["games"] as $game) {
   foreach ($game as $key => $value) {
      echo $value; 
   }
}

这应该为您提供游戏部分中每个数组中的值。

编辑:在评论中回答其他问题

要获得类似 的游戏的特定值,id不需要第二个 foreach 循环。而是执行以下操作:

foreach($json["games"] as $game) {
   echo $game['id'];
}
于 2013-10-04T02:34:54.817 回答
1

您只需将其作为关联数组访问。

$json['games'][0]['id'] // This is 2013020004
$json['games'][1]['id'] // This is 2013020005

您可以像这样循环游戏:

foreach($json['games'] as $game){
   print_r($game);
}
于 2013-10-04T02:32:53.917 回答
0

如果您尝试访问它,

    [0] => Array
        (
            [id] => 2013020004
            [gs] => 5
            [ts] => WEDNESDAY 10/2
            [tsc] => final
            [bs] => FINAL
            [bsc] => final
        )

    [1] => Array
        (
            [id] => 2013020005
            [gs] => 5
            [ts] => WEDNESDAY 10/2
            [tsc] => final
            [bs] => FINAL
            [bsc] =>
        )

..那么你做对了。唯一的问题是您试图回显一个数组。尝试使用print_r($value). 如果你想要一个特定的值,比如 id。你可以echo $value['id'];

于 2013-10-04T02:33:51.090 回答
0

我认为您可以使用递归 array_key_search的众多示例之一。这样你就可以简单地做:

$firstGame = array_search_key(0, $array);
$firstGameId = $firstGame["id"];
于 2013-10-04T02:40:44.257 回答