php 如何获取 JSON 属性名称

Bra*_*gae 2 php oop json

我有一个以下 JSON 文件:

{"ver":2,"sb":[some array],"ld":[some array] ,"hd":[some array]}
Run Code Online (Sandbox Code Playgroud)

我尝试使用下一个代码来获取属性名称:

$path='./datafiles/jsonTest.json';
$data = json_decode(file_get_contents($path));
$properties=get_object_vars($data);
foreach($properties as $propName){
    echo $propName.'<br>';
}
Run Code Online (Sandbox Code Playgroud)

但结果我得到:

2
Array
Array
Array
Run Code Online (Sandbox Code Playgroud)

当我需要时:

'ver'
'sb'
'ld'
'hd'
Run Code Online (Sandbox Code Playgroud)

有人能帮我吗?谢谢!

Sta*_*sik 5

您还可以尝试使用 json_decode 给您一个关联数组

$path='./datafiles/jsonTest.json';
$data = json_decode(file_get_contents($path),true);
foreach($data as $name => $value){
    echo $name.'<br>';
}
Run Code Online (Sandbox Code Playgroud)