Sbbcarl Posted May 16, 2017 Share Posted May 16, 2017 (edited) 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 May 16, 2017 by Sbbcarl Quote Link to comment https://forums.phpfreaks.com/topic/303949-php-nested-json-from-flat-json/ Share on other sites More sharing options...
requinix Posted May 17, 2017 Share Posted May 17, 2017 It'll be easier to work this into the code you already have than to give you a bunch of stuff and have you adapt it. What is your code? Quote Link to comment https://forums.phpfreaks.com/topic/303949-php-nested-json-from-flat-json/#findComment-1546569 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.