数据库
首页 > 数据库> > php-从数据库表创建数组“树”

php-从数据库表创建数组“树”

作者:互联网

我的PHP功能

function generateMenu($parent, $level, $menu, $utype) {
    global $db;
    $tree = array();
    $stmt = $db->prepare("select id, parent, name FROM navigation WHERE menu=? AND user_type=?") or die($db->error);
    $stmt->bind_param("ii", $menu, $utype) or die($stmt->error);
    $stmt->execute() or die($stmt->error);
    $stmt->store_result();
    $meta = $stmt->result_metadata();
    $stmt->bind_result($id, $parent, $name);
    while ($stmt->fetch()) {
        $arr[$id] = array(
            'name' => $name, 
            'parent' => $parent
        );
        if (!array_key_exists($parent,$arr) and $parent != 0) {
            $arr[$parent][$id] = $id;
        }
    }
    $stmt->close();
}

从数据库表生成以下数组. [1],[2] …-是li项目的ID

Array (
    [1] => Array ( 
        [name] => Parent1
        [parent] => 0
    ) 
    [2] => Array ( 
        [name] => Parent2
        [parent] => 0 
    )
    [3] => Array (
        [name] => Parent3 
        [parent] => 0 
    )
    [4] => Array ( 
        [name] => Child1 of P1
        [parent] => 1
    ) 
    [5] => Array (
        [name] => Child2 of P1
        [parent] => 1
    ) 
)

我想要做的就是像这样创建菜单

<ul>
  <li><a href="?page=1">Parent1</a>
    <ul>
      <li><a href="?page=4">Child1 of P1</a></li>
        ...

第二个功能是从该数组生成菜单.但是我知道在将该数组发送到第二个函数之前,我需要将其转换为多维树数组.我不知道该怎么做.

这是第二个功能

function olLiTree($tree) {
    $out = '<ul>';

    foreach($tree as $key => $value) {
        $out.= '<li>';

        if (is_array($value)) {
            $out.= $key . olLiTree($value);
        } else {
            $out.= $value;
        }

        $out.= '</li>';
    }

    $out.= '</ul>';

    return $out;
}

db结构

解决方法:

函数array_key_exists()需要两个参数,您只能传递一个.我想你的意思是:

while(list($id, $parent, $name) = mysql_fetch_assoc($results)) {
    $tree[$id] = array(
        'name' => $name, 
        'children' => array(), 
        'parent' => $parent
    );
    if (!array_key_exists($parent,$tree)) {
        $tree[$parent]['children'][$id] = $id;
    }
}

您更改了答案,所以我认为问题不在于array_keys_exists.无论如何,您都可以尝试使用这种方式通过MaxDB获取数据:

function generateMenu($parent, $level, $menu, $utype) {
    global $db;
    $tree = array();
    $stmt = $db->prepare("select id, parent, name FROM navigation WHERE menu=? AND user_type=?") or die($db->error);
    $stmt->bind_param("ii", $menu, $utype) or die($stmt->error);
    $stmt->execute() or die($stmt->error);
    $stmt->store_result();
    $meta = $stmt->result_metadata();

    $stmt->bind_result($id, $parent, $name);
    while ($stmt->fetch()) {
        $tree[$id] = array(
            'name' => $name, 
            'children' => array(), 
            'parent' => $parent
        );
        if (!array_key_exists($parent,$tree)) {
            $tree[$parent]['children'][$id] = $id;
        }
    }
    $stmt->close();
    print_r($tree);
}

对于第二个功能,我认为@jeroen具有严格性,您需要使用this anwser.

标签:tree,hierarchical-data,arrays,mysql,php
来源: https://codeday.me/bug/20191102/1987866.html