如何使用递归将数据保存到数据库

时间:2014-03-10 04:46:46

标签: php mysql recursion

我的mysql表看起来像这样:

page_id     parnt_id    page_name
------------------------------------
1           0           AboutUs
2           1           Contact_us
3           2           Home
4           2           Currear

我有一个看起来像这样的数组:

Array
(
    [0] => Array
        (
            [id] => 1
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 3
                                        )

                                )
                        )
                )
        )
)

如何使用递归将上述数组添加到数据库中?

4 个答案:

答案 0 :(得分:2)

$all_pages = $given_array;

function save_page($page, $parent_id = 0) {

  $page_id = $page['id'];
  $sql = "INSERT INTO table_name (page_id, parent_id)
         VALUES ($page_id, $parent_id)";
  mysql_query($sql);

  if(array_key_exists('children', $page)) {
    save_page($page['children'], $page['id']);
  }
}

save_page($all_pages);

答案 1 :(得分:2)

您可以使用一些辅助类,而不是自己进行递归;假设以下数据:

$data = [[
        'id' => 1,
        'name' => 'About_Us',
        'children' => [[
                'id' => 2,
                'name' => 'Contact_Us',
                'children' => [[
                        'id' => 3,
                        'name' => 'Home',
                ], [
                        'id' => 4,
                        'name' => 'Careers',
                ]],
        ]],
]];

您可以使用RecursiveArrayIterator为您执行迭代,以便您可以在一个简单的循环中执行数据库工作:

$stmt = $db->prepare('INSERT INTO pages (page_id, parent_id, page_name) 
    VALUES (?, ?, ?)');

$it = new RecursiveIteratorIterator(
    new RecursiveArrayIterator($data), 
    RecursiveIteratorIterator::CHILD_FIRST
);

foreach ($it as $key => $node) {
    if (!isset($node['id'])) {
        continue; // skip nodes without an id field
    }

    if ($it->getDepth() > 1) {
        // go two levels down for the parent
        $parent = $it->getSubIterator($it->getDepth() - 2)->current();
    } else {
        $parent = null;
    }

    $stmt->execute([$node['id'], $parent ? $parent['id'] : 0, $node['name']]);
}

答案 2 :(得分:1)

如下所示:

function importPages($pages, $parent_id = 0) {
  foreach ($pages as $page) {
    insertToDB($page['id'], $parent_id);
    foreach ($page['children'] as $child) {
      importPages($child, $page['id']);
    }
  }
}

答案 3 :(得分:1)

function recurInsert($node, $parent) {
    $que = "INSERT INTO `table_name` (page_id, parnt_id) VALUES ('".$node['id']."', '".$parent."')";
    // process this query, you can add page name also, 
    // but I don't know where you store it
    if (is_array($node['children'])) {
        foreach ($children as $child) {
            recurInsert($child, $node['id']);
        }
    }
}
// start recursion 
recurInsert($your_array, 0);
哦,我刚注意到@xdazz,来自印度的家伙已​​经回答了几乎相同的