1

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:

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) """" } 
6
  • output this var_dump($array); Commented Oct 17, 2015 at 3:18
  • $array[0]->atv still give me blank. Commented Oct 17, 2015 at 3:26
  • Do I need to strip the quotes before setting the array keys/values? Commented Oct 17, 2015 at 3:26
  • @PatrickLewis probably. or access like array['"atv"'] Commented Oct 17, 2015 at 3:27
  • That did work, but the value includes the quotes, so looks like i need to clean it up at the beginning and remove the exra ones. Commented Oct 17, 2015 at 3:30

2 Answers 2

1

I was also facing the same problem.

Problem:

The array was retrieved in Database [saved as a JSON encoded variable].

I was not getting the array element by key like $arr['key']

Solution:

Tried everything, no success.

Lastly, tried json_decode() and json_encode().

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

Note that second parameter TRUE is very important. Otherwise, you will get object returned.

And it worked like charm.

Sign up to request clarification or add additional context in comments.

Comments

0

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'];

Here is the Demo

7 Comments

he's not posting how he initialized the array, just the contents of a var_dump and probably a print_r
He is trying to get values from array, as he says: "I am trying to get particular values, like home team and away team and the scores, but I cannot get the values.". I did put this code,'cause if he can't get the values, it could be due to a bad array initialization.
see the comments on the post. his keys and values had " around them for some reason. He didn't actually post code, only the results of a print_r and a var_dump. Neither of those print commas. You're not wrong though
You are correct too, but if you check his answer, all it seems that it was a bad initialization. I think if we only give answer for patching things, the people that is asking will have all code wrong, in my personal opinion, I like to give complete answers.
Probably. it depends on how he was building his array. I'm all for giving complete answers but he didn't provide enough information to make that assumption. imo :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.