gpt4 book ai didi

PHP数组到Json树格式

转载 作者:行者123 更新时间:2023-12-04 20:33:22 26 4
gpt4 key购买 nike

我想转换 php数组进入 用于 JavaScript InfoVis Toolkit 格式的 json 字符串 .

目标:
InfoVis Demo Tree

Json规范格式:
InfoVis-loading and serving JSON data

我有这个 php 数组: $my_array :

Array 
(
[item_1] => Array
(
[id] => item_1_ID
[name] => item_1_NAME
[data] => item_1_DATA
[children] => Array
(
[door] => Array
(
[id] => door_ID
[name] => door_NAME
[data] => door_DATA
[children] => Array
(
[mozart] => Array
(
[id] => mozart_ID
[name] => mozart_NAME
[data] => mozart_DATA
[children] => Array
(
[grass] => Array
(
[id] => grass_ID
[name] => grass_NAME
[data] => yes
)

[green] => Array
(
[id] => green_ID
[name] => green_NAME
[data] => no
)

[human] => Array
(
[id] => human_ID
[name] => human_NAME
[data] => human_DATA
[children] => Array
(
[blue] => Array
(
[id] => blue_ID
[name] => blue_NAME
[data] => blue_DATA
[children] => Array
(
[movie] => Array
(
[id] => movie_ID
[name] => movie_NAME
[data] => yes
)

)

)

)

)

)

)

)

)

[beat] => Array
(
[id] => beat_ID
[name] => beat_NAME
[data] => yes
)

[music] => Array
(
[id] => music_ID
[name] => music_NAME
[data] => no
)

)

)

)

现在如果我 json_encode($my_array);
{ 
"item_1": {
"id": "item_1_ID",
"name": "item_1_NAME",
"data": "item_1_DATA",
"children": {
"door": {
"id": "door_ID",
"name": "door_NAME",
"data": "door_DATA",
"children": {
"mozart": {
"id": "mozart_ID",
"name": "mozart_NAME",
"data": "mozart_DATA",
"children": {
"grass": {
"id": "grass_ID",
"name": "grass_NAME",
"data": "yes"
},
"green": {
"id": "green_ID",
"name": "green_NAME",
"data": "no"
},
"human": {
"id": "human_ID",
"name": "human_NAME",
"data": "human_DATA",
"children": {
"blue": {
"id": "blue_ID",
"name": "blue_NAME",
"data": "blue_DATA",
"children": {
"movie": {
"id": "movie_ID",
"name": "movie_NAME",
"data": "yes"
}
}
}
}
}
}
}
}
},
"beat": {
"id": "beat_ID",
"name": "beat_NAME",
"data": "yes"
},
"music": {
"id": "music_ID",
"name": "music_NAME",
"data": "no"
}
}
}
}

但是对于 InfoVis,当前的 json 输出 (json_encode($my_array)) 有 3 个问题:
  • 未使用 [ ]
  • 'children' 数组的键名是
  • 数组项带有它们的键名 -> 示例:“item_1”:{ ....

  • 让我指出这个问题,所以也许你可以帮助一个函数来转换这个 json 字符串:

    看到这片 json_encode($my_array) 输出:
    {  
    "item_1": {
    "id": "item_1_ID",
    "name": "item_1_NAME",
    "data": "item_1_DATA",
    "children": {
    "door": {
    "id": "door_ID",

    1.问题1:
    {  
    "item_1": {

    we have to remove those keys like: "item_1":



    2.问题2:
    "children": {  
    "door": {
    "id": "door_ID",

    the correct code for this should be:


    "children": [  
    {
    "id": "door_ID",......

    "door": was removed... because it`s a key

    "children": { => becomes" "children": [



    “ child ”的一个工作示例:
    "children": [  
    {
    "id": "grass_ID",
    "name": "grass_NAME",
    "data": "yes"
    },
    {
    "id": "green_ID",
    "name": "green_NAME",
    "data": "no"
    }
    ]

    澄清 WORKING Json InfoVis 格式的完整示例:
    json = {
    id: "node02",
    name: "0.2",
    children: [{
    id: "node13",
    name: "1.3",
    children: [{
    id: "node24",
    name: "2.4"
    }, {
    id: "node222",
    name: "2.22"
    }]
    }, {
    id: "node125",
    name: "1.25",
    children: [{
    id: "node226",
    name: "2.26"
    }, {
    id: "node237",
    name: "2.37"
    }, {
    id: "node258",
    name: "2.58"
    }]
    }, {
    id: "node165",
    name: "1.65",
    children: [{
    id: "node266",
    name: "2.66"
    }, {
    id: "node283",
    name: "2.83"
    }, {
    id: "node2104",
    name: "2.104"
    }, {
    id: "node2109",
    name: "2.109"
    }, {
    id: "node2125",
    name: "2.125"
    }]
    }, {
    id: "node1130",
    name: "1.130",
    children: [{
    id: "node2131",
    name: "2.131"
    }, {
    id: "node2138",
    name: "2.138"
    }]
    }]
    };

    清楚明白吗?

    希望任何人都可以帮助我..我正在为此工作几天!

    谢谢你。

    最佳答案

    试试这个快速转换功能

    function fixit($yourArray) {
    $myArray = array();
    foreach ($yourArray as $itemKey => $itemObj) {
    $item = array();
    foreach ($itemObj as $key => $value) {
    if (strtolower($key) == 'children') {
    $item[$key] = fixit($value);
    } else {
    $item[$key] = $value;
    }
    }
    $myArray[] = $item;
    }
    return $myArray;
    }

    $fixed = fixit($my_array);
    $json = json_encode($fixed);

    关于PHP数组到Json树格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8730577/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com