簡體   English   中英

帶關聯數組的PHP遞歸(菜單結構)

[英]PHP Recursion with Associative Array (Menu structure)

我今天一直在撞牆。 我有一個mysql表,其中包含具有遞歸父/子關系的菜單項列表。 我需要創建一個與其匹配的關聯數組。

這是我的代碼,下面是我的問題的解釋。

MySQL數據庫

CREATE TABLE IF NOT EXISTS `menus` (
  `sectionid` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `parentid` int(10) unsigned NOT NULL DEFAULT '0',
  `name` varchar(128) NOT NULL DEFAULT '',
  PRIMARY KEY (`sectionid`)
);

INSERT INTO `menus` (`sectionid`, `parentid`, `name`) VALUES
(1, 0,'DOWNLOADS'),(4, 1,'Player Scenarios'),
(5, 1,'Co Op Scenarios'),(9, 1,'MAPS');

PHP代碼

function get_db_children($parentid) {

  $mysqli = mysqli_open();
  $sqlStr = "select sectionid as childid from menus where parentid=$parentid order by sectionid";
  $result = $mysqli->query($sqlStr);
  $mysqli->close();

  return $result;

}

function get_children(&$node) {

  foreach ($node as $key=>$value) {
    $result = get_db_children($key);
    while ($row = $result->fetch_assoc()) {
       $tmp = array($row['childid'] => array());
       $node[$key][$row['childid']]= get_children($tmp);
    }
  }
  return $node;
}

==========================================

從我的主腳本中調用上述函數,如下所示:

$categories[0] = array ();
get_children($categories);
print "Categories=".print_r($categories);
exit;

==============

我的問題。

我的問題是返回數組的結構不是我想要的那樣。

上面的代碼返回:

Categories=
Array ( [0] => Array 
        ( [1] => Array 
          ( [1] => Array 
            ( [4] => Array 
              ( [4] => Array ( ) )
              [5] => Array 
              ( [5] => Array ( ) )
              [9] => Array 
              ( [9] => Array ( ) )
            )
          )
        )
      ) 

我想要的是:

Categories=
Array ( [0] => Array 
          ( [1] => Array 
            (
              ( [4] => Array ( ) )
              ( [5] => Array ( ) )
              ( [9] => Array ( ) )
            )
          )
      )

空數組表示沒有子級。

我不知道如何擺脫關鍵值的雙重影響。 :(

如果有人可以幫助,將不勝感激。

打ap

稍微改變方法可以解決我的問題。 我沒有通過引用傳遞$categories數組,而是傳遞了父菜單項的ID並返回了該數組。

下面的代碼使用與我的OP中相同的SQL數據和SQL訪問器函數get_db_children($parentid) 我相信這可以改善,但至少我有結果。

@Josh,謝謝您的輸入。

PHP函數

function get_twigs($nodeid) {

  $result = get_db_children($nodeid);
  if ($result->num_rows != 0) {
    while ($row = $result->fetch_assoc()) {
      $node[$row['childid']] = array();
      $node[$row['childid']] = get_twigs($row['childid']);
    }
  } else {
    $node=NULL; // or any other 'no children below me indicator.
  }
  return $node;
}

function get_branch($nodeid) {

  $node[$nodeid] = array();
  $node[$nodeid] = get_twigs($nodeid);
  return $node;
}

PHP呼叫者

$categories = get_branch(0);

終於成功了。

:)

您是通過引用傳遞$node ,而不是這樣做$node[$key][$row['childid']] = get_children($tmp); 你應該真的在做

$value[$row['childid']] = get_children($tmp)

要么

$node[$row['childid']] = get_children($temp)

但是如果使用第二個選項,則必須在第一次調用get_children傳遞$categories[0] (這是您進行重復調用時的操作)

更新:

請嘗試解釋其原因...

如您所見,第一個條目(0)沒有重復。 僅在第一個重復調用是您的問題開始的地方之后,其原因是因為您是通過引用傳遞子數組,因此第二次[作為示例]調用get_children$node var實際上已經引用了嵌套部分

array(
   0=>array(
      1=>array(/*this is what $node actually is the second time around*/
         /*when you say $node[$key][$row['childid']] what you are actually saying is assign the value to array[0][1][1][4] = array())*/
      )
   )
);

我希望這有幫助。 如果我能想到一種解釋它的方法,請回來並更新...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM