1

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) enter image description here

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

2
  • don't you actually want to use accountMainID, not accountID in => 'items' => recursiveNode($ledgerID, $row3['accountID'])? Commented Sep 30, 2019 at 3:39
  • actually if the data's has a child noted under it, it create a dependencies based on accountID. you can see the expected result accountMainID is dependent on accountID above it. Plus there is parents function and I will call this function to call a child and sub-child. Commented Sep 30, 2019 at 3:48

2 Answers 2

1
 function fetch_account ($dbresult, $ledgerID, $accountID) { $result = array_filter($dbresult, function ($something) use ($ledgerID, $accountID) { if ( $something['accountMainID'] == $accountID && $something['accountLedgerID'] == $ledgerID ) { return true; } return false; }); return array_values($result); } function recursiveNode($ledgerID,$accountID){ $testArray = [ [ 'accountLedgerID' => 'LA1', 'accountID' => 'LA95', 'accountMainID' => 'LA5', 'accountName' => 'SubGroup RunDeposit 1', 'active' => 'y' ], [ 'accountLedgerID' => 'LA1', 'accountID' => 'LA2', 'accountMainID' => 'LA5', 'accountName' => 'SubGroup RunDeposit 2', 'active' => 'y' ], [ 'accountLedgerID' => 'LA1', 'accountID' => 'LA125', 'accountMainID' => 'LA2', 'accountName' => 'Sub x2 Group RunDeposit 2', 'active' => 'y' ], [ 'accountLedgerID' => 'LA1', 'accountID' => 'LA6', 'accountMainID' => 'LA125', 'accountName' => 'Sub x3 Group RunDeposit 2', 'active' => 'y' ] ]; $someArray = fetch_account($testArray, $ledgerID, $accountID); $subArray = array(); $i = 0; while (!empty($someArray[$i]) && $row3 = $someArray[$i]) { $subArray[] = array( 'accountLedgerID' => $row3['accountLedgerID'], 'accountID' => $row3['accountID'], 'accountMainID' => $row3['accountMainID'], 'accountName' => $row3['accountName'], 'active' => $row3['active'], 'items' => recursiveNode($ledgerID, $row3['accountID']) ); $i++; } return $subArray; } $myArray = recursiveNode('LA1', 'LA5'); $result = json_encode($myArray); echo $result; 
Sign up to request clarification or add additional context in comments.

2 Comments

See if this makes sense for your use-case, I've replaced the database query portion but should be the same result. You can safely ignore the fetch_account since it's just trying to mimic the database query object.
Thank you, after see your sample seem json_encode need to be outside the recursive function.
0

Breaking down the problem first, I think the recursive function isn't returning anything. I think this is indicative when your result has 'items' as null.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.