PHP 数组不能使用键获取值

PHP array cannot use key to get value

我有这个数组:

Array ( ["id"] => 2015020052 ["gs"] => 5 ["ts"] => "THURSDAY 10/15" 
["tsc"] => "final" ["bs"] => "FINAL" ["bsc"] => "final" 
["atn"] => "Chicago" ["atv"] => "blackhawks" ["ats"] => "1" 
["atc"] => "" ["htn"] => "Washington" ["htv"] => "capitals" 
["hts"] => "4" ["htc"] => "winner" ["pl"] => true ["rl"] => true 
["vl"] => true ["gcl"] => true ["gcll"] => true ["ustv"] => "" 
["catv"] => "" ) 

我正在尝试获取特定值,例如主队和客队以及比分,但我无法获取这些值。

我正在尝试这个:

echo "away team is ". $array['atv'];

但我刚刚明白 客队是

我错过了什么????

var_dump 给我这个:

Array vs array(21) { [""id""]=> string(10) "2015020051" 
[""gs""]=> string(1) "5" [""ts""]=> string(16) ""THURSDAY 10/15"
[""tsc""]=> string(7) ""final"" [""bs""]=> string(7) ""FINAL"" 
[""bsc""]=> string(7) ""final"" [""atn""]=> string(8) ""Ottawa""
[""atv""]=> string(10) ""senators"" [""ats""]=> string(3) ""0"" 
[""atc""]=> string(2) """" [""htn""]=> string(12) ""Pittsburgh""
[""htv""]=> string(10) ""penguins"" [""hts""]=> string(3) ""2""
[""htc""]=> string(8) ""winner"" [""pl""]=> string(4) "true" 
[""rl""]=> string(4) "true" [""vl""]=> string(4) "true" 
[""gcl""]=> string(4) "true" [""gcll""]=> string(4) "true" 
[""ustv""]=> string(2) """" [""catv""]=> string(2) """" } 

你没有做正确的关联数组,一定是这样的:

<?php

$array = [
        'id' => 2015020052,
        'gs' => 5,
        'ts' => "THURSDAY 10/15",
        'tsc' => "final", 
        'bs' => "FINAL",
        'bsc' => "final",
        'atn' => "Chicago",
        ...
    ];

现在可以得到值:

echo "away team is ". $array['atv'];

这是Demo

我也遇到了同样的问题

问题:

在数据库中检索到数组[保存为 JSON 编码变量]。

我没有像 $arr['key']

那样按键获取数组元素

解决方案:

什么都试过了,没有成功。

最后,尝试了 json_decode() and json_encode()

$arr = json_decode(json_encode($arr), TRUE);

注意第二个参数TRUE非常重要。否则,您将返回对象。

而且效果很好。