Thursday 21 December 2017

php - Problem accessing the value of the json with a point in its name





I need to access the value of the json that contains a point in its
name.




I would like to access the
"proy_sim.name" field but I do not know
how



{ 
"prsp_sol":
[
{
"proy_sim.name": "Vehículos",
"prsp_def.name":
"TRACTOR"
}

]

}


Answer




After decoding with
json_decode() you'll realize that there is an additional array
you're not accounting for:



$json = '{
"prsp_sol": [

{
"proy_sim.name": "Vehículos",
"prsp_def.name":
"TRACTOR"

}

]
}';

$decoded = json_decode($json, true); // true makes
it an array
print_r($decoded);

echo
$decoded['prsp_sol'][0]['proy_sim.name'];
//-----------------------^
additional nested
array



The
output:



Array
(

[prsp_sol] => Array
(
[0] => Array
(

[proy_sim.name] => Vehículos

[prsp_def.name] =>
TRACTOR
)

)
)

Vehículos


Here
is an href="http://sandbox.onlinephpfunctions.com/code/03b803e7502618e4debfe5e5d2e941e9cf0de61b"
rel="nofollow
noreferrer">example




The
point in the name is irrelevant.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...