I have a folowing JSON file:
{"ver":2,"sb":[some array],"ld":[some array] ,"hd":[some array]}
I try to ger property names with next code:
$path='./datafiles/jsonTest.json';
$data = json_decode(file_get_contents($path));
$properties=get_object_vars($data);
foreach($properties as $propName){
echo $propName.'<br>';
}
but as result I get:
2
Array
Array
Array
when I need:
'ver'
'sb'
'ld'
'hd'
Can someone help me? Thanks!
If you don't need the resulting output as an object, you coud use the array version of json_decode
$data = json_decode(file_get_contents($path), true);
$properties = array_keys($data);
Have you tried the key?
foreach($properties as $key => $propName){
echo $key.'<br>';
}
you can also try using json_decode to give you an associative array
$path='./datafiles/jsonTest.json';
$data = json_decode(file_get_contents($path),true);
foreach($data as $name => $value){
echo $name.'<br>';
}
You can either use reflection to get the properties names (http://php.net/manual/en/reflectionclass.getproperties.php) - which would be the elegant way - or you decide not to decode the Json data and extract the names and values with manual string operations.