[英]PHP array cannot use key to get value
I have this array: 我有这个数组:
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"] => "" )
I am trying to get particular values, like home team and away team and the scores, but I cannot get the values. 我试图获得一些特定的价值观,例如主队和客队以及得分,但我无法获得这些价值观。
I am trying this: 我正在尝试:
echo "away team is ". $array['atv'];
But i just get away team is 但是我只是逃脱团队
What am i missing???? 我想念什么????
var_dump gives me this: 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) """" }
You are not doing a correct associative array, this must be like this: 您没有在做正确的关联数组,这必须是这样的:
<?php
$array = [
'id' => 2015020052,
'gs' => 5,
'ts' => "THURSDAY 10/15",
'tsc' => "final",
'bs' => "FINAL",
'bsc' => "final",
'atn' => "Chicago",
...
];
Now you can get the value: 现在,您可以获得值:
echo "away team is ". $array['atv'];
I was also facing the same problem. 我也面临着同样的问题。
Problem: 问题:
The array was retrieved in Database [saved as a JSON encoded variable]. 该数组是在数据库中检索的(保存为JSON编码变量)。
I was not getting the array element by key like $arr['key']
我没有像
$arr['key']
这样的键获取数组元素
Solution: 解:
Tried everything, no success. 尝试了一切,没有成功。
Lastly, tried json_decode() and json_encode() . 最后,尝试json_decode()和json_encode() 。
$arr = json_decode(json_encode($arr), TRUE);
Note that second parameter TRUE
is very important. 请注意,第二个参数
TRUE
非常重要。 Otherwise, you will get object returned. 否则,您将返回对象。
And it worked like charm. 它像魅力一样运作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.