JSON数据不应具有数字键值,结果不会按预期结果显示

时间:2017-11-02 14:58:45

标签: php json

我使用递归创建嵌套集模型的JSON。我的结果没有像预期的那样,因为这个JSON帮助我生成一棵树。括号不是必需的JSON。

我正在尝试创建一个像这样http://fperucic.github.io/treant-js/examples/evolution-tree/example6.js的json。我对 nodeStructure:{}

感兴趣

问题:

  1. 每个孩子都有{}但需要[]

  2. 不需要数字键

  3. json键不应该出现像" text"," children," name" ,它应该没有引号

  4. 在线编译器:https://3v4l.org/UsXPv

    <?php
      $category = '{"9":{"id":"9","btc_mlm_user_id":"0","lft":"1","rht":"16","lvl":"0","name":"Root","created":"2017-06-27 05:56:11","modified":"2017-06-27 05:56:11","first_name":"","last_name":"","username":""},"42":{"id":"42","btc_mlm_user_id":"25","lft":"2","rht":"13","lvl":"1","name":"naresh","created":"2017-11-02 10:22:24","modified":"2017-11-02 10:22:24","first_name":"","last_name":"","username":"naresh"},"44":{"id":"44","btc_mlm_user_id":"27","lft":"3","rht":"4","lvl":"2","name":"rahul1","created":"2017-11-02 10:25:53","modified":"2017-11-02 10:25:53","first_name":"","last_name":"","username":"rahul1"},"45":{"id":"45","btc_mlm_user_id":"28","lft":"5","rht":"6","lvl":"2","name":"rahul123","created":"2017-11-02 10:27:19","modified":"2017-11-02 10:27:19","first_name":"","last_name":"","username":"rahul123"},"46":{"id":"46","btc_mlm_user_id":"29","lft":"7","rht":"12","lvl":"2","name":"kapil1","created":"2017-11-02 10:28:20","modified":"2017-11-02 10:28:20","first_name":"","last_name":"","username":"kapil1"},"47":{"id":"47","btc_mlm_user_id":"30","lft":"8","rht":"11","lvl":"3","name":"priya12","created":"2017-11-02 10:30:30","modified":"2017-11-02 10:30:30","first_name":"","last_name":"","username":"priya12"},"48":{"id":"48","btc_mlm_user_id":"31","lft":"9","rht":"10","lvl":"4","name":"amit12","created":"2017-11-02 10:32:00","modified":"2017-11-02 10:32:00","first_name":"","last_name":"","username":"amit12"},"43":{"id":"43","btc_mlm_user_id":"26","lft":"14","rht":"15","lvl":"1","name":"roshan","created":"2017-11-02 10:24:27","modified":"2017-11-02 10:24:27","first_name":"","last_name":"","username":"roshan"}}';
    
      function tree($data, $left = 0, $right = null) 
      {
        $tree = array();
        foreach ($data as $key => $value) 
        {
          if ($value['lft'] == $left + 1 && (is_null($right) || $value['rht'] < $right)) 
          {
            $tree[$key]['text']  = ['name' => $value['name']];
            $tree[$key]['children'] = tree($data, $value['lft'], $value['rht']);
            $left = $value['rht'];
          }
        }
        return $tree;
      }
    
      $tree = tree(json_decode($category, true));
      echo json_encode($tree);
    

    输出:

    {
      "9": {
        "text": {
          "name": "Root"
        },
        "children": {
          "42": {
            "text": {
              "name": "naresh"
            },
            "children": {
              "44": {
                "text": {
                  "name": "rahul1"
                },
                "children": []
              },
              "45": {
                "text": {
                  "name": "rahul123"
                },
                "children": []
              },
              "46": {
                "text": {
                  "name": "kapil1"
                },
                "children": {
                  "47": {
                    "text": {
                      "name": "priya12"
                    },
                    "children": {
                      "48": {
                        "text": {
                          "name": "amit12"
                        },
                        "children": []
                      }
                    }
                  }
                }
              }
            }
          },
          "43": {
            "text": {
              "name": "roshan"
            },
            "children": []
          }
        }
      }
    }
    

    必需的输出:

    {
      text: {
        name: "Root"
      },
      children: [{
        text: {
          name: "naresh"
        },
        children: [{
          text: {
            name: "rahul1"
          },
          children: [
            []
          ],
          text: {
            name: "rahul123"
          },
          children: [
            []
          ],
          text: {
            name: "kapil1"
          },
          children: [{
            text: {
              name: "priya12"
            },
            children: [{
              text: {
                name: "amit12"
              },
              children: [
                []
              ]
            }]
          }]
        }],
        text: {
          name: "roshan"
        },
        children: [
          []
        ]
      }]
    }
    

    以下是我的MySql记录,我将在此处向您显示 $ category json in start。

    enter image description here

1 个答案:

答案 0 :(得分:3)

如果您想获得output you linked,而不是问题中的那个(as pointed by @RoryMcCrossan无效,因为每个对象包含多个相等的密钥),那么您可以将代码更改为此:

function tree($data, $left = 0, $right = null) {
    $tree = array();
    foreach ($data as $key => $value) {
        if ($value['lft'] == $left + 1 && (is_null($right) || $value['rht'] < $right)) {
            $child = []; // Let's make a new child
            $child['text'] = ['name' => $value['name']]; // The text is required
            $childTree = tree($data, $value['lft'], $value['rht']); // Let's find its children
            if (!empty($childTree)) { // If it has children
                $child['children'] = $childTree; // Let's save the children
            }
            $tree[] = $child; // Put the child in the tree
            $left = $value['rht'];
        }
    }
    return $tree;
}

$tree = tree(json_decode($category, true))[0]; // Since there's only one root, you want the first element of the tree

以下是完整代码:https://3v4l.org/AYCGt

根据你的说法,这只会给你留下一个问题,钥匙不应该在它们周围引用。虽然我不太了解你的动机并且它应该使用Javascript中的引号,但你可以使用preg_replace进行一些替换,如下所示:

echo preg_replace('/"(\w+)":/','$1:',json_encode($tree));

这将是完整的代码:https://3v4l.org/ZaXip