Skip to content
Advertisement

Get stdClass Object value

I’m trying to get a value from stdClass Object array with no success.

Here is the code I’m running:

 $myjson =
 '{"2":{"label":"","value":"","type":null,"validation":null,"required":null},
 "6":{"label":"files","value":"getThisValue","type":"file0","validation":null,"required":null},
 "3":{"label":"location","value":"val3","type":"hidden","validation":"","required":"0"}
 ,"0":{"custom3":"zz","value":"","label":""},"1":{"custom3":"zz","value":"","label":""}
 }';

$json = json_decode($myjson);

echo $json[6]->'value';

This is doesn’t work, If I Print_r the JSON after decoding (print_r($json)), the array will look like this:

stdClass Object ( 
    [2] => stdClass Object ( [label] => [value] =>
[type] => [validation] => [required] => ) 
    [6] => stdClass Object (
[label] => files [value] => getThisValue [type] => file0 [validation]
=> [required] => ) 
    [3] => stdClass Object ( [label] => location [value] => val3 [type] => hidden [validation] => [required] => 0 ) 
    [0]
=> stdClass Object ( [custom3] => zz [value] => [label] => ) 
    [1] => stdClass Object ( [custom3] => zz [value] => [label] => ) )

I need the Value: getThisValue. Any idea how I can get it? (I tried many options with no success).

Advertisement

Answer

Try echo $json["6"]["value"]; But for this you have to use json_decode($myjson, true); true, to get an array.

Because it’s going to be two arrays inside each other and not an object you have to use 2 brackets.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement