PHP循环递归函数

PHP循环递归函数,php,loops,while-loop,Php,Loops,While Loop,我有这个循环函数,它用来获取数据,然后以json格式显示结果。在$subArray[]中,我尝试再次调用循环函数,以便它可以读取下面是否有任何子节点。但结果似乎并不像我预期的那样 function recursiveNode($ledgerID,$accountID){ global $ehorsObj; $subArray = array(); $query_get_subchild = " SELECT accountLedgerID, accountID, accountMa

我有这个循环函数,它用来获取数据,然后以json格式显示结果。在
$subArray[]
中,我尝试再次调用循环函数,以便它可以读取下面是否有任何子节点。但结果似乎并不像我预期的那样

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;
}
它显示结果(如下图所示)

我期望的结果是这样的

[
{
accountLedgerID:“LA1”,
帐号:“LA95”,
accountMainID:“LA5”,
accountName:“子组RunDeposit 1”,
活动:“y”
},
{
accountLedgerID:“LA1”,
帐号:“LA2”,
accountMainID:“LA5”,
accountName:“子组RunDeposit 2”,
活动:“y”,
项目:[
{
accountLedgerID:“LA1”,
帐号:“LA125”,
accountMainID:“LA2”,
accountName:“子x2组运行存款2”,
活动:“y”,
项目:[
{
accountLedgerID:“LA1”,
帐号:“LA6”,
accountMainID:“LA125”,
账户名称:“子x3集团RunDeposit 2”,
活动:“y”,
项目:[]
}	
]
}	
]
}

]
首先分析问题,我认为递归函数没有返回任何内容。我认为当您的结果“items”为空时,这是一种指示

您真的不想在=>
'items'=>recursiveNode($ledgerID,$row3['accountID'])
中使用
accountMainID
,而不是
accountID
?事实上,如果数据下有一个子项,它会基于
accountID
创建依赖项。您可以看到预期结果
accountMainID
取决于它上面的
accountID
。另外还有parents函数,我将调用此函数来调用子函数和子函数。看看这对您的用例是否有意义,我已经替换了数据库查询部分,但应该是相同的结果。您可以安全地忽略
fetch\u account
,因为它只是试图模拟数据库查询对象。谢谢,在看到您的示例后,seem
json\u encode
需要在递归函数之外。

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;