0

I have a hash array in the form

[{"sector":"Consumer, Cyclical","ticker":"NWY","entity":"New York & Co","New_York_&_Co":[{"count":1,"entity":"New York"}],"type":"SCap"}]

I am trying to list the values corresponding to key value "entity" of all occurences. I did use json_decode,

$testJson = json_decode('[{"sector":"Consumer, Cyclical","ticker":"NWY","entity":"New York & Co","New_York_&_Co":[{"count":1,"entity":"New York"}],"type":"SCap"}]');

When I tried `echo var_dump($testJson[0]);
it shows the output in form

array
  0 => 
  object(stdClass)[438]
  public 'sector' => string 'Consumer, Cyclical' (length=18)
  public 'ticker' => string 'NWY' (length=3)
  public 'entity' => string 'New York & Co' (length=13)
  public 'New_York_&_Co' => 
    array
      0 => 
        object(stdClass)[439]
          ...
  public 'type' => string 'SCap' (length=4)

but echo var_dump($testJson[0]->entity) or echo var_dump($testJson[0]->sector) gives me error.. "Trying to get property of non-object" ..What Could I be doing wrong?

4
  • Is the code you tested missing single quotes around the string value, as in the sample? (It should be: json_decode('[{"sector":"Consumer,....) If the posted code isn't representative, please update it. Commented Jul 18, 2012 at 7:46
  • Please check the output of var_dump that I specified in the question part.. it shows another array, right? could that be the isue?? array 0=> Commented Jul 18, 2012 at 8:04
  • the input "[{"sector":"Consumer, Cyclical","ticker":"NWY","entity":"New York & Co","New_York_&_Co":[{"count":1,"entity":"New York"}],"type":"SCap"}]" is coming from an array, that should be string right? Commented Jul 18, 2012 at 8:05
  • My point is the sample code wasn't legal PHP. Since it wasn't representative of the code you ran, it also won't have the same issues. (Consider picking a meaningful username so at-replies will work.) Commented Jul 18, 2012 at 8:12

1 Answer 1

1
$testJSon= json_decode('[{"sector":"Consumer, Cyclical","ticker":"NWY","entity":"New York & Co","New_York_&_Co":[{"count":1,"entity":"New York"}],"type":"SCap"}]');
//var_dump($testJSon);
echo $testJSon['0']->sector; // will output `Consumer, Cyclical`
echo $testJSon[0]->{'sector'}; // will also output same

and you can convert it in array and get the values like:

$testJSon= json_decode('[{"sector":"Consumer, Cyclical","ticker":"NWY","entity":"New York & Co","New_York_&_Co":[{"count":1,"entity":"New York"}],"type":"SCap"}]',true);
echo $testJSon[0]['entity']; // will return "New York & Co"
Sign up to request clarification or add additional context in comments.

5 Comments

no it doesnt...it gives me the error "Trying to get property of non-object" ... The above solution is the one for javascript...that wrks fine... it doesnt wrk this way in php
y is zero inserted inside single quotes?
@user1371896 its working fine in php for me in both ways, did you pasted the real json you are using ?
What could be causin the error "Trying to get property of non-object"
I've pasted the code working here (codepad.viper-7.com/PRITQo), you can compare the code and output.

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.