Jump to content

Recommended Posts

I have a database query that provides me the output of some employee data. I want to use this data to pass to a plugin that generates an org chart. There are a few fields in the JSON object that I am pulling down which are:
 

    FirstName
    LastName
    EmployeeID
    ManagerEmployeeID
    Manager Name

The data is returned as flat JSON object with no nesting or coloration between employees and their managers in the hierarchy.

Since I am unable to change the output of the source data (the database query), I am trying to figure out a way to nest the data so that the JSON output becomes a nested output.

My goal is to take this array and nest it based on the ManagerID and EmployeeID so I can make a tree hierarchy.

**Example Data:**

 

•    Tom Jones
       o    Alice Wong
       o    Tommy J.
•    Billy Bob
       o    Rik A.
         ♣    Bob Small
         ♣    Small Jones
       o    Eric C.

**My flat data example:**
 

        {
            "FirstName": "Tom"
            "LastName": "Jones"
            "EmployeeID": "123"
            "ManagerEmployeeID": ""
            "Manager Name": ""
        },
        {
            "FirstName": "Alice"
            "LastName": "Wong"
            "EmployeeID": "456"
            "ManagerEmployeeID": "123"
            "Manager Name": "Tom Jones"
        },
        {
            "FirstName": "Tommy"
            "LastName": "J."
            "EmployeeID": "654"
            "ManagerEmployeeID": "123"
            "Manager Name": "Tom Jones"
        },
        {
            "FirstName": "Billy"
            "LastName": "Bob"
            "EmployeeID": "777"
            "ManagerEmployeeID": ""
            "Manager Name": ""
        },
        {
            "FirstName": "Rik"
            "LastName": "A."
            "EmployeeID": "622"
            "ManagerEmployeeID": "777"
            "Manager Name": "Billy Bob"
        },
        {
            "FirstName": "Bob"
            "LastName": "Small"
            "EmployeeID": "111"
            "ManagerEmployeeID": "622"
            "Manager Name": "Rik A."
        },
        {
            "FirstName": "Small"
            "LastName": "Jones"
            "EmployeeID": "098"
            "ManagerEmployeeID": "622"
            "Manager Name": "Rik A"
        },
        {
            "FirstName": "Eric"
            "LastName": "C."
            "EmployeeID": "222"
            "ManagerEmployeeID": "777"
            "Manager Name": "Billy Bob"
        }

    

**Example Desired Output:**

    
    [
      {
        "FirstName": "Tom",
        "LastName": "Jones",
        "EmployeeID": "123",
        "ManagerEmployeeID": "",
        "Manager Name": "",
        "employees": [
          {
            "FirstName": "Alice",
            "LastName": "Wong",
            "EmployeeID": "456",
            "ManagerEmployeeID": "123",
            "Manager Name": "Tom Jones"
          },
          {
            "FirstName": "Tommy",
            "LastName": "J.",
            "EmployeeID": "654",
            "ManagerEmployeeID": "123",
            "Manager Name": "Tom Jones"
          }
        ]
      },
      {
        "FirstName": "Billy",
        "LastName": "Bob",
        "EmployeeID": "777",
        "ManagerEmployeeID": "",
        "Manager Name": "",
        "employees": [
          {
            "FirstName": "Rik",
            "LastName": "A.",
            "EmployeeID": "622",
            "ManagerEmployeeID": "777",
            "Manager Name": "Billy Bob",
            "employees": [
              {
                "FirstName": "Bob",
                "LastName": "Small",
                "EmployeeID": "111",
                "ManagerEmployeeID": "622",
                "Manager Name": "Rik A."
              },
              {
                "FirstName": "Small",
                "LastName": "Jones",
                "EmployeeID": "098",
                "ManagerEmployeeID": "622",
                "Manager Name": "Rik A"
              }
            ]
          },
          {
            "FirstName": "Eric",
            "LastName": "C.",
            "EmployeeID": "222",
            "ManagerEmployeeID": "777",
            "Manager Name": "Billy Bob"
          }
        ]
      }
    ]

Essentially I am trying to create a nested JSON output from a flat object using the `EmployeeID` and `ManagerEmployeeID` as the links between the two.

What is the best way to solve something like this with PHP?

 

I have this code working in a Javascript function but I am pretty new to PHP and not sure what similarities there are to re-write the function in PHP.

 

https://jsfiddle.net/87ztvk8m/

Edited by Sbbcarl
Link to comment
https://forums.phpfreaks.com/topic/303949-php-nested-json-from-flat-json/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.