Johan
Johan

Reputation: 79

JSON data should not have key value in numeric and result not coming as per expected result

I am creating JSON with nested set model using recursion. My result is not coming as expected, as this JSON helps me to generate a tree. The brackets are not coming as in required JSON.

i am trying to create a json like this http://fperucic.github.io/treant-js/examples/evolution-tree/example6.js . i am interested in nodeStructure: { }

Issue:

  1. every children has { } but required [ ]

  2. Not required numeric keys

  3. json keys should not come in quotes like "text", "children, "name" , its should come witout quotes

Online compiler: 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);

Output:

{
  "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": []
      }
    }
  }
}

Required output:

{
  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: [
      []
    ]
  }]
}

Here is my MySql records which are i am fetching here to show you in $category json in start.

enter image description here

Upvotes: 0

Views: 409

Answers (1)

Piyin
Piyin

Reputation: 1834

If you want to get the output you linked, rather than the one in your question (which is invalid as pointed by @RoryMcCrossan, because it contains multiple equal keys per object), then you can change your code to this:

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

Here's the full code: https://3v4l.org/AYCGt

That just leaves you with one problem, according to you, the keys shouldn't have quotes around them. Although I don't really know your motives and it should work with the quotes in Javascript, you could do some replacements using preg_replace, like this:

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

This would be the complete code: https://3v4l.org/ZaXip

Upvotes: 3

Related Questions