php对嵌套数组进行反向迭代以创建节点/边缘json

时间:2017-02-08 14:55:49

标签: php arrays json

在我的数组$data中我试图将其转换为内部有三个数组的json:nodesedgescategorys

categorys:将第一个元素名称作为常规类别。
nodes:将拥有属于“Nbdoc”属性的每个子类别及其中包含的项目数。
edges:将是节点之间的链接。例如,subcategory1subcategory3共有一个项目“1001”,因此size将为1。 subcategory3subcategory1中的任何一个都可以作为源,或者可以作为目标,它与顺序无关。
id属性只是从增量顺序1开始的唯一ID。我们为每个节点以及每个边(链接)赋予唯一的id。
样本数据:

Array
(
    [mycategory1] => Array
        (
            [subcategory1] => Array
                (
                    [0] => 1001
                    [1] => 1002
                    [2] => 1003
                )

            [subcategory2] => Array
                (
                    [0] => 1004
                    [1] => 1005
                    [2] => 1006
                    [3] => 1007
                    [4] => 1008                    
                )

            [subcategory3] => Array
                (
                    [0] => 1008
                    [1] => 1001
                    [2] => 1005
                    [3] => 1006
               )
          )

    [mycategory2] => Array
        (
            [subcategory4] => Array
                (
                    [0] => 1008
                    [1] => 1005
                    [2] => 1003
               )
          )
)

我现在拥有的东西:

<?php

protected function myfunction()
    {

        $data = somefunction(...);
        $nodes = array();
        $categorys = array();
        $edges = array();
        $nodesArray = array();
        $nodeId = 1;
        $edgeId = 1;
        $json = array();

        foreach ($data as $category => $value) {
            $categorys[] = array('category' => $category, 'icon' => null);
            print_r($categorys);

            foreach ($value as $node => $nodeValue) {
                $nodesArray[$nodeId] = $nodeValue;
                print_r($nodeId);
                die();
                $nodes[] = array('name' => $node, 'category' => $category, 'id' => $nodeId, 'NbDoc' => count($nodeValue));
                for ($i = 1; $i < $nodeId; $i++) {
                    $count = array_count_values(array_merge($nodesArray[$i], $nodesArray[$nodeId]));
                    $size = 0;
                    foreach ($count as $key => $edgeSize) {
                        if ($edgeSize > 1) {
                            $size++;
                        }
                    }
                    if ($size > 0) {
                        $edges[] = array('target' => $nodeId, 'source' => $i, 'id' => $edgeId, 'size' => $size);
                        $edgeId++;
                    }
                }

                $nodeId++;

            }
        }
        $json['categorys'] = $categorys;
        $json['nodes'] = $nodes;
        $json['edges'] = $edges;

        return $json;
    }

所需的json输出:

{
    "categorys": [
        {
            "category": "mycategory1",
            "icon": null
        },
        {
            "category": "mycategory2",
            "icon": null
        }           
    ],
    "nodes": [
        {
            "name": "subcategory1",
            "category": "mycategory1",
            "id": 1,
            "NbDoc": 3
        },
        {
            "name": "subcategory2",
            "category": "mycategory1",
            "id": 2,
            "NbDoc": 5
        },
        {
            "name": "subcategory3",
            "category": "mycategory1",
            "id": 3,
            "NbDoc": 4
        },
        {
            "name": "subcategory4",
            "category": "mycategory2",
            "id": 4,
            "NbDoc": 3
        }       
    ],
    "edges": [
        {
            "target": 3,
            "source": 1,
            "id": 1,
            "size": 1
        },
        {
            "target": 4,
            "source": 1,
            "id": 2,
            "size": 1
        },
        {
            "target": 3,
            "source": 2,
            "id": 3,
            "size": 3
        },
        {
            "target": 4,
            "source": 2,
            "id": 4,
            "size": 2
        },
        {
            "target": 4,
            "source": 3,
            "id": 5,
            "size": 2
        }
    ]
}

0 个答案:

没有答案