I had this looping function and what it does to fetch data then display the result in json format. In $subArray[] i try to call the looping function again so it can read if got any sub-nodes underneath. But seem the result not display as I expected to be.
function recursiveNode($ledgerID,$accountID){ global $ehorsObj; $subArray = array(); $query_get_subchild = " SELECT accountLedgerID, accountID, accountMainID, accountName, active FROM tblAccAccounts WHERE accountMain = 'y' AND accountSub = 'y' AND accountMainID = '".$accountID."' AND accountLedgerID = '".$ledgerID."' ORDER BY accountName "; $GetResult = $ehorsObj->FetchData($query_get_subchild, $ehorsObj->DEFAULT_PDO_CONNECTIONS); while ($row3 = $GetResult->fetch()) { $subArray[] = array('accountLedgerID' => $row3['accountLedgerID'], 'accountID' => $row3['accountID'], 'accountMainID' => $row3['accountMainID'], 'accountName' => $row3['accountName'], 'active' => $row3['active'], 'items' => recursiveNode($ledgerID, $row3['accountID'])); } header("Content-type: application/json"); $result = json_encode($subArray); echo $result; } it show the result (as image below) 
and the result I expected to be like this
[ { accountLedgerID: "LA1", accountID: "LA95", accountMainID: "LA5", accountName: "SubGroup RunDeposit 1", active: "y" }, { accountLedgerID: "LA1", accountID: "LA2", accountMainID: "LA5", accountName: "SubGroup RunDeposit 2", active: "y", item: [ { accountLedgerID: "LA1", accountID: "LA125", accountMainID: "LA2", accountName: "Sub x2 Group RunDeposit 2", active: "y", items: [ { accountLedgerID: "LA1", accountID: "LA6", accountMainID: "LA125", accountName: "Sub x3 Group RunDeposit 2", active: "y", items: [ ] } ] } ] } ]
accountMainID, notaccountIDin =>'items' => recursiveNode($ledgerID, $row3['accountID'])?accountID. you can see the expected resultaccountMainIDis dependent onaccountIDabove it. Plus there is parents function and I will call this function to call a child and sub-child.