[英]Hirearchy tree json using php?
I have json like this. 我有这样的json。 i have try this Convert PHP array to JSON tree but always fail when i create.
我已经尝试过将PHP数组转换为JSON树,但是在创建时总是失败。
{
"name": "Level 7",
"value": "4.5",
"parents": [
{
"name": "Level 5",
"value": "Gery",
"parents": [
{
"name": "Level 2",
"value": "Robert"
},
{
"name": "Level 1",
"value": "Rio"
}
]
},
{
"name": "Level 6",
"value": "GEO",
"parents": [
{
"name": "Level 3",
"value": "Lion"
},
{
"name": "Level 4",
"value": "Pery"
}
]
}
]
}
How to create json hierachy from php? 如何从php创建json hierachy?
$data = array(
array('name' => 'Level 1', 'value' => 'Rio', 'parents' => 'Level 7/Level 5'),
array('name' => 'Level 2', 'value' => 'Robet', 'parents' => 'Level 7/Level 5'),
array('name' => 'Level 3', 'value' => 'Lion', 'parents' => 'Level 7/Level 6'),
array('name' => 'Level 4', 'value' => 'Pery', 'parents' => 'Level 7/Level 6'),
array('name' => 'Level 5', 'value' => 'Gery', 'parents' => 'Level 7'),
array('name' => 'Level 6', 'value' => 'GEO', 'parents' => 'Level 7'),
array('name' => 'Level 7', 'value' => '4.5', 'parents' => 'none'),
);
If the json is stored as a string, you can convert it to a PHP object with json_decode
. 如果json以字符串形式存储,则可以使用
json_decode
将其转换为PHP对象。
$object = json_decode($json);
If it's a PHP object, you can convert it to a string with json_encode
. 如果它是PHP对象,则可以使用
json_encode
将其转换为字符串。
$string = json_decode($object);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.