0

我今天一直在用头撞墙。我有一个 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 ( ) )
            )
          )
      )

空数组表示没有孩子。

我不知道如何摆脱关键值的双重影响。:(

如果有人可以提供帮助,将不胜感激。

干杯,小睡

4

2 回答 2

1

稍微改变方法解决了我的问题。我没有通过引用传递$categories数组,而是传递父菜单项的 ID 并返回数组。

get_db_children($parentid)下面的代码使用与我的 OP 中给出的相同的 SQL 数据和 SQL 访问器函数。我确信这可以改进,但至少我得到了我的结果。

@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);

终于搞定了。

:)

于 2013-02-16T14:30:17.537 回答
0

您正在通过$node引用传递,因此$node[$key][$row['childid']] = get_children($tmp);您应该真正做而不是做

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

或者

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

但是如果你选择第二个选项,你将不得不$categories[0]在第一次调用时传递get_children(这是你在进行重复调用时所做的)

更新:

我试着解释一下原因……

如您所见,第一个条目(0)没有重复。只有在第一次重复调用之后才是您的问题开始的地方,原因是您通过引用传入子数组,因此第二次 [作为示例] 当您调用var 实际上已经引用了嵌套get_children部分$node

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())*/
      )
   )
);

我希望这有帮助。如果我能想出一种方法来解释它,面糊生病回来更新......

于 2013-02-16T10:15:56.887 回答