PHP数组到Json树格式



我想把一个php数组转换成一个json字符串来使用JavaScript InfoVis Toolkit格式

目的:InfoVis演示树

Json规范格式:infovis -加载和服务JSON数据

我有这个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个问题:

  1. 未使用[]
  2. '子'数组具有键名
  3. 用键名数组项->示例:"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": {  

我们必须删除这些键,比如:"item_1":

<标题> 2。问题2:
"children": {  
            "door": {  
                "id": "door_ID",  
正确的代码应该是:
"children": [  
            {  
                "id": "door_ID",......  

"door":被移除…因为它是一个键

"children":{=>变为"children": [

]

'children'的工作示例:

"children": [  
    {  
        "id": "grass_ID",  
        "name": "grass_NAME",  
        "data": "yes"  
    },  
    {  
        "id": "green_ID",  
        "name": "green_NAME",  
        "data": "no"  
    }  
]  

澄清一个完整的工作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不区分数组(数字键)和关联数组(字符串键)。它们都是数组。Javascript确实有区别。因为你正在使用字符串键,它们必须在JS中作为对象({})来完成。

你不能告诉json_encode忽略数组中的键(例如你的'children'子数组)。这意味着生成的JSON是不是与原来的PHP结构相同-你现在已经改变了键名。

你必须处理你的数组并将所有子数组键转换为数字:

grass -> 0
green -> 1
etc...

以便json-encode可以看到它是一个数字键的PHP数组,这意味着它将生成一个实际的javavscript数组([]),而不是一个对象({})。

另一种选择是编写自己的json编码器来动态地为您做这些。

这是记录在案的行为。当使用json_encode对JSON进行字符串化时,关联数组将产生一个对象字面值。更新您的原始数组结构,以表示您想要的结果,而不是破坏生成的JSON表示,或者为每个对象围绕json_encode包装您自己的解决方案。

编辑:尝试清理操作

的代码
$original = <your original data-array>; // assumed, I reversed your encoded JSON as test data
// Start by stripping out the associative keys for level 1
$clean = array_values($original);
// Then recursively descend array, and do the same for every children-property encountered
function &recursiveChildKeysCleaner(&$arr) {
    // If $arr contains 'children'...
    if (array_key_exists('children', $arr)) {
        /// ...strip out associative keys
        $arr['children'] = array_values($arr['children']);
        // ...and descend each child
        foreach ($arr['children'] as &$child) {
            recursiveChildKeysCleaner($child);
        }
    }
    return $arr;
}
foreach ($clean as &$item) {
    recursiveChildKeysCleaner($item);
}
unset($item);
echo json_encode($clean);

输出
[{
    "id": "item_1_ID",
    "name": "item_1_NAME",
    "data": "item_1_DATA",
    "children": [{
        "id": "door_ID",
        "name": "door_NAME",
        "data": "door_DATA",
        "children": [{
            "id": "mozart_ID",
            "name": "mozart_NAME",
            "data": "mozart_DATA",
            "children": [{
                "id": "grass_ID",
                "name": "grass_NAME",
                "data": "yes"
            },
            {
                "id": "green_ID",
                "name": "green_NAME",
                "data": "no"
            },
            {
                "id": "human_ID",
                "name": "human_NAME",
                "data": "human_DATA",
                "children": [{
                    "id": "blue_ID",
                    "name": "blue_NAME",
                    "data": "blue_DATA",
                    "children": [{
                        "id": "movie_ID",
                        "name": "movie_NAME",
                        "data": "yes"
                    }]
                }]
            }]
        }]
    },
    {
        "id": "beat_ID",
        "name": "beat_NAME",
        "data": "yes"
    },
    {
        "id": "music_ID",
        "name": "music_NAME",
        "data": "no"
    }]
}]

相关内容

  • 没有找到相关文章

最新更新