SOLVED!
A nifty little function created by a friend has solved this issue for me. He actually created it in Javascript but I've translated it over to PHP. I'll supply both below.
The PHP Version first:
$JSON = '[{"id":1,"children":[{"id":2,"children":[{"id":3},{"id":4}]},{"id":5}]}]';
$cleanJSON = json_decode($JSON,true);
$a_newTree = array();
function recurseTree($structure,$previousLeft)
{
global $a_newTree; // Get global Variable to store results in.
$indexed = array(); // Bucket of results.
$indexed['id'] = $structure['id']; // Set ID
$indexed['left'] = $previousLeft + 1; // Set Left
$lastRight = $indexed['left'];
$i_count = 0;
if ($structure['children'])
{
foreach ($structure['children'] as $a_child)
{
$lastRight = recurseTree($structure['children'][$i_count],$lastRight);
$i_count++;
}
}
$indexed['right'] = $lastRight + 1; // Set Right
array_push($a_newTree,$indexed); // Push onto stack
return $indexed['right'];
}
recurseTree($cleanJSON[0],0);
print_r($a_newTree);
Fantastic little function outputs the exact array required.
OK, For the original JAVASCRIPT version my friend wrote, see below:
<html>
<head>
<title>Experiment</title>
<script type="text/javascript">
/* initial structure */
var struct = [
{
"id": 1,
"children": [{
"id": 2,
"children": [{
"id": 3
}, {
"id": 4
}]
}, {
"id": 5
}]
}
];
function experiment() {
/* stores all results */
var all = [];
/* kick off the recursive method */
handleNode(struct[0], 0, all);
/* print the results to browser debugger console*/
console.log(all);
}
function handleNode(node, previousLeft, all) {
/* create and store the new entry (bucket to put left, right, and id ) */
var indexed = {};
all.push(indexed);
indexed.id = node["id"];
indexed.left = previousLeft + 1;
var lastRight = indexed.left;
/* here we do the recursion for every child */
for (var x in node["children"]) {
lastRight = handleNode(node["children"][x], lastRight, all);
}
/* once all children have been iterated over we can store the rigth */
indexed.right = lastRight + 1;
/* return the newly updated right for this bucket */
return indexed.right;
}
/* run the experiment */
experiment();
</script>
</head>
<body>
</body>
</html>
Using Google Chrome you can see the results in the Console window. (CTRL-SHIFT-i).