在mysql中保存树数组

时间:2014-08-23 05:07:40

标签: php mysql cakephp-2.3

Array
(
    [Root] => Array
        (
            [Parent0] => Array
                (
                    [Child0] => Child0
                )

            [Parent1] => Array
                (
                    [Child1] => Child1
                )

        )

)

上面的树数组需要保存在带有父ID的mysql中,所以结果应如下所示:

id  parent_id   name
1   0           Root
2   1           Parent0
3   2           Child0
4   1           Parent1
5   4           Child1

任何人请告诉我如何使用php将上述结果保存在mysql中。

提前感谢您的快速回复。

3 个答案:

答案 0 :(得分:1)

试试这个:已编辑

         $c=0;
    $x;
    foreach($array1 as $key=>$val){
        if(is_array($val)){
            echo "insert $key with parent id $c<br>";
            $c++;
            $x=$c; 
            foreach($val as $key1=>$val1){          
                echo " insert $key1 with parent id $x<br>";
   $c++;

                if(is_array($val1)){
                    foreach($val1 as $key2=>$val2){
                      echo "insert $key2 with parent id $c<br>";
                         $c++;
                        getlevel($val2,$c);
                    }
                }/* else{
                    echo "else insert $key1 with parent id $c<br>";
                    $c++; */
                }

            }

        }


    function getlevel($value,$c1){
        if(is_array($value)){
            foreach($value as $keyV=>$Value){
                echo " insert $keyV with parent id $c1<br>";
                $c1++;
                if(is_array($Value)){
                    getlevel($Value,$c1);
                }
            }
        }
    }

哪里有写过echo替换你的sql.Hope它有帮助。$array是你的数组。

答案 1 :(得分:1)

在任何深度级树的完美工作脚本下面。

$root_id;
$parent_id = 0;

foreach ( $tree_array as $root_key => $root_value ) {

    $parent_id = insertRecord($root_key, $parent_id);
    $root_id = $parent_id;

    if ( is_array($root_value) ) {
        foreach ( $root_value as $parent_key => $parent_value ) {

            $parent_id = insertRecord($parent_key, $root_id);
            $keep_parent_id = $parent_id;

            if ( is_array($parent_value) ) {
                foreach ( $parent_value as $child_key => $child_value ) {

                    $parent_id = insertRecord($child_key, $keep_parent_id);
                    getlevel($child_value, $parent_id);
                }
            }
        }
    }
}

function getlevel($sub_childs, $new_parent_id) {
    $keep_new_parent_id = $new_parent_id;

    if ( is_array($sub_childs) ) {
        foreach ( $sub_childs as $sub_child => $sub_child_sub ) {

            $new_parent_id = insertRecord($sub_child, $keep_new_parent_id);
            if ( is_array($sub_child_sub) ) {
                getlevel($sub_child_sub, $new_parent_id);
            }
        }
    }
}

function insertRecord($name, $parent_id) {
    $q = "insert into xtable set name = '".$name."',  parent_id = '".$parent_id."'";
    mysql_query($q);

    $folder_id = mysql_insert_id();

    return $folder_id;
}

感谢大家的努力。

答案 2 :(得分:-1)

如果您想要实现父子树,您可以使用n深度

获取以下代码
 Automobile
        Fuel
            Gasoline
            Diesel
        Maintenance
    Food
        Fish
        Pork

首先创建一个名为“categories”的数据库表,其中包含字段。

- category_id (PK int)
- parent_id (int)
- title (varchar)

<?php
    $connect = mysql_connect("localhost", "root", "") or die ( mysql_error() );
    mysql_select_db("test");
    $nav_query = mysql_query("SELECT * FROM `categories` ORDER BY `category_id`") or die( mysql_error() );
    $tree = ""; // Clear the directory tree
    $depth = 1; // Child level depth.
    $top_level_on = 1; // What top-level category are we on?
    $exclude = array(); // Define the exclusion array
    array_push($exclude, 0); // Put a starting value in it

   while ( $nav_row = mysql_fetch_array($nav_query) )
   {
      $goOn = 1; // Resets variable to allow us to continue building out the tree.
      for($x = 0; $x < count($exclude); $x++ ) // Check to see if the new item has been used
      {
          if ( $exclude[$x] == $nav_row['category_id'] )
          {
             $goOn = 0;
             break; // Stop looking b/c we already found that it's in the exclusion list and we can't continue to process this node
          }
      }
      if ( $goOn == 1 )
      {
          $tree .= $nav_row['title'] . "<br>"; // Process the main tree node
          array_push($exclude, $nav_row['category_id']); // Add to the exclusion list
          if ( $nav_row['category_id'] < 6 )
          { $top_level_on = $nav_row['category_id']; }

          $tree .= build_child($nav_row['category_id']); // Start the recursive function of building the child tree
       }
    }

   function build_child($oldID) // Recursive function to get all of the children...unlimited depth
   {
       global $exclude, $depth; // Refer to the global array defined at the top of this script
       $tempTree = "";
       $child_query = mysql_query("SELECT * FROM `categories` WHERE parent_id=" . $oldID);
       while ( $child = mysql_fetch_array($child_query) )
       {
          if ( $child['category_id'] != $child['parent_id'] )
          {
             for ( $c=0;$c<$depth;$c++ ) // Indent over so that there is distinction between levels
             { $tempTree .= " "; }
             $tempTree .= "- " . $child['title'] . "<br>";
             $depth++; // Incriment depth b/c we're building this child's child tree (complicated yet???)
             $tempTree .= build_child($child['category_id']); // Add to the temporary local tree
             $depth--; // Decrement depth b/c we're done building the child's child tree.
             array_push($exclude, $child['category_id']); // Add the item to the exclusion list
          }
       }
       return $tempTree; // Return the entire child tree
     }

     echo $tree;

?>

你可以复制粘贴上面的代码,你就完成了:)