递归数据库充填多维数组 [英] recursively database filling with multidimensional array

查看:147
本文介绍了递归数据库充填多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想递归访问id字段和libelle现场填写我的数据库。我想下面的脚本我只填写ID和libelle的第一级。(其中idTypeCaategorie = 0)。这只是我的一个阵列的一部分。

I would like to recursively access the id field and libelle field to fill my database. I tried the following script I fill only the first level of id and libelle.(where idTypeCaategorie = 0). It's just a part of my array.

我要填补我的数据表'菜单1'='libelle'和'ID'= ID,并为libelle这将是巨大的串联与孩子libelle父libelle每个子级别的只有两个领域。 -

I want fill just two field in my data table 'menu1'='libelle' and 'id'=id, and for each sub-level of libelle it would be great to concatenate the parent libelle with the child libelle. –

我的数据表stucure:

my datatable stucure :

结构列>>>>> ID(INT(10)| newspaper_index(= 8)| menu_level(INT)| MENU1()| MENU2(的)|菜单3()| menu4(的)| menu5()| menu6(的)| menu7(*)|;其strucure MENU1是libelle的第一级和MENU2到menu7是libelle儿童

structure columns >>>>> id(int(10) | newspaper_index(=8) |menu_level(int) | menu1 ()|menu2() |menu3() | menu4 ()| menu5() | menu6() | menu7(*)| ; its the strucure menu1 is libelle of the first level and menu2 to menu7 are libelle children

为例1:UNIVERS贝贝(菜单1)(ID:.....)menu_level = 1

exemple 1: Univers Bebe(menu1) (id : .....) menu_level=1

为例2:UNIVERS贝贝(菜单1)> Lait的等黑麦(MENU2)(ID:....)menu_level = 2

exemple 2: Univers Bebe(menu1) > Lait et cereale(menu2) (id :....) menu_level=2

为例3:UNIVERS贝贝(菜单1)> Lait的等黑麦(MENU2)>牛乳1er酒店年龄(菜单3)(ID:.....)menu_level = 3

exemple 3: Univers Bebe(menu1) > Lait et cereale(menu2) > lait 1er age (menu3)(id:.....) menu_level=3

* = VARCHAR
 我希望把从阵列信息在我的数据库与此上述结构
例如数组:

*=varchar I want put information from array in my database with this above structure Example Array:


Array
(
    [0] => Array
        (
            [id] => 288
            [libelle] => Univers Bébé
            [idTypeCategorie] => 0
            [nomImage] => 1905.jpg
            [ponderation] => 1
            [fils] => Array
                (
                    [0] => Array
                        (
                            [id] => 2228
                            [libelle] => Laits & céréales
                            [idTypeCategorie] => 1
                            [nomImage] => 2002.png
                            [ponderation] => 1
                            [mea] => 2067
                            [fils] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 1024
                                            [libelle] => Lait 1er âge
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 1
                                            [nbProduits] => 9
                                            [fils] => Array
                                                (
                                                )

                                        )

                                    [1] => Array
                                        (
                                            [id] => 1025
                                            [libelle] => Lait 2Úme âge
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 2
                                            [nbProduits] => 10
                                            [fils] => Array
                                                (
                                                )

                                        )

                                    [2] => Array
                                        (
                                            [id] => 1026
                                            [libelle] => Croissance
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 3
                                            [nbProduits] => 19
                                            [fils] => Array
                                                (
                                                )

                                        )

                                    [3] => Array
                                        (
                                            [id] => 290
                                            [libelle] => Petit déjeuner
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 4
                                            [nbProduits] => 9
                                            [fils] => Array
                                                (
                                                )

                                        )

                                    [4] => Array
                                        (
                                            [id] => 2595
                                            [libelle] => Céréale
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 5
                                            [nbProduits] => 10
                                            [fils] => Array
                                                (
                                                )

                                        )

                                )

                        )

                    [1] => Array
                        (
                            [id] => 2230
                            [libelle] => Repas de Bébé
                            [idTypeCategorie] => 1
                            [nomImage] => 1974.png
                            [ponderation] => 2
                            [mea] => 2482
                            [fils] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 3651
                                            [libelle] => Repas à base de légumes
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 3
                                            [nbProduits] => 28
                                            [fils] => Array
                                                (
                                                )

                                        )

                                    [1] => Array
                                        (
                                            [id] => 3656
                                            [libelle] => Purée pour bébé
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 4
                                            [nbProduits] => 8
                                            [fils] => Array
                                                (
                                                )

                                        )

                                    [2] => Array
                                        (
                                            [id] => 3652
                                            [libelle] => Repas à base de viande
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 5
                                            [nbProduits] => 29
                                            [fils] => Array
                                                (
                                                )

                                        )

                                    [3] => Array
                                        (
                                            [id] => 3653
                                            [libelle] => Repas à base de poisson
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 6
                                            [nbProduits] => 16
                                            [fils] => Array
                                                (
                                                )

                                        )

                                    [4] => Array
                                        (
                                            [id] => 3654
                                            [libelle] => Repas à base de volaille
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 7
                                            [nbProduits] => 18
                                            [fils] => Array
                                                (
                                                )

                                        )

                                    [5] => Array
                                        (
                                            [id] => 3657
                                            [libelle] => Repas BIO
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 8
                                            [nbProduits] => 4
                                            [fils] => Array
                                                (
                                                )

                                        )

                                )

                        )

                    [2] => Array
                        (
                            [id] => 3660
                            [libelle] => Diner & Soupe
                            [idTypeCategorie] => 1
                            [nomImage] => 1989.png
                            [ponderation] => 3
                            [mea] => 3764
                            [fils] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 3661
                                            [libelle] => Pour le diner
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 1
                                            [nbProduits] => 16
                                            [fils] => Array
                                                (
                                                )

                                        )

                                    [1] => Array
                                        (
                                            [id] => 3662
                                            [libelle] => Soupe pour bébé
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 2
                                            [nbProduits] => 9
                                            [fils] => Array
                                                (
                                                )

                                        )

                                )

                        )

                    [3] => Array
                        (
                            [id] => 2227
                            [libelle] => Goûter et desserts
                            [idTypeCategorie] => 1
                            [nomImage] => 2003.png
                            [ponderation] => 4
                            [mea] => 309
                            [fils] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 294
                                            [libelle] => Jus de fruits
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 1
                                            [nbProduits] => 1
                                            [fils] => Array
                                                (
                                                )

                                        )

                                    [1] => Array
                                        (
                                            [id] => 1021
                                            [libelle] => Purée de fruits
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 2
                                            [nbProduits] => 26
                                            [fils] => Array
                                                (
                                                )

                                        )

                                    [2] => Array
                                        (
                                            [id] => 3663
                                            [libelle] => Compote
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 3
                                            [nbProduits] => 18
                                            [fils] => Array
                                                (
                                                )

                                        )

                                    [3] => Array
                                        (
                                            [id] => 1023
                                            [libelle] => Biscuit
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 4
                                            [nbProduits] => 3
                                            [fils] => Array
                                                (
                                                )

                                        )

                                    [4] => Array
                                        (
                                            [id] => 1022
                                            [libelle] => Yaourt pour bébé
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 5
                                            [nbProduits] => 8
                                            [fils] => Array
                                                (
                                                )

                                        )

                                    [5] => Array
                                        (
                                            [id] => 3664
                                            [libelle] => CrÚme dessert
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 6
                                            [nbProduits] => 11
                                            [fils] => Array
                                                (
                                                )

                                        )

                                )

                        )

                    [4] => Array
                        (
                            [id] => 295
                            [libelle] => Couche-culottes
                            [idTypeCategorie] => 1
                            [nomImage] => 2029.png
                            [ponderation] => 5
                            [mea] => 1713
                            [fils] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 2576
                                            [libelle] => 3 - 6 Kg
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 1
                                            [nbProduits] => 7
                                            [fils] => Array
                                                (
                                                )

                                        )

                                    [1] => Array
                                        (
                                            [id] => 308
                                            [libelle] => 2 - 9 Kg
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 2
                                            [nbProduits] => 6
                                            [fils] => Array
                                                (
                                                )

                                        )

                                    [2] => Array
                                        (
                                            [id] => 309
                                            [libelle] => 7 - 18 Kg
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 3
                                            [nbProduits] => 10
                                            [fils] => Array
                                                (
                                                )

                                        )

                                    [3] => Array
                                        (
                                            [id] => 310
                                            [libelle] => 9 - 25 Kg
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 4
                                            [nbProduits] => 13
                                            [fils] => Array
                                                (
                                                )

                                        )

                                    [4] => Array
                                        (
                                            [id] => 311
                                            [libelle] => Culottes
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 6
                                            [nbProduits] => 10
                                            [fils] => Array
                                                (
                                                )

                                        )

                                    [5] => Array
                                        (
                                            [id] => 3829
                                            [libelle] => Lingettes
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 7
                                            [nbProduits] => 18
                                            [fils] => Array
                                                (
                                                )

                                        )

                                )

                        )

                    [5] => Array
                        (
                            [id] => 296
                            [libelle] => Toilette & soins
                            [idTypeCategorie] => 1
                            [nomImage] => 2031.png
                            [ponderation] => 6
                            [mea] => 968
                            [fils] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 312
                                            [libelle] => Lingettes
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 1
                                            [nbProduits] => 18
                                            [fils] => Array
                                                (
                                                )

                                        )

                                    [1] => Array
                                        (
                                            [id] => 313
                                            [libelle] => Savons & gels lavants
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 3
                                            [nbProduits] => 7
                                            [fils] => Array
                                                (
                                                )

                                        )

                                    [2] => Array
                                        (
                                            [id] => 314
                                            [libelle] => Autres soins
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 5
                                            [nbProduits] => 6
                                            [fils] => Array
                                                (
                                                )

                                        )

                                    [3] => Array
                                        (
                                            [id] => 4183
                                            [libelle] => Coton pour bébé
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 6
                                            [nbProduits] => 4
                                            [fils] => Array
                                                (
                                                )

                                        )

                                )

                        )

                    [6] => Array
                        (
                            [id] => 2229
                            [libelle] => Puériculture
                            [idTypeCategorie] => 1
                            [nomImage] => 2030.png
                            [ponderation] => 7
                            [mea] => 36176
                            [fils] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 2732
                                            [libelle] => HygiÚne et bain pour bébé
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 7
                                            [nbProduits] => 2
                                            [fils] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 2214
            [libelle] => Fruits & légumes
            [idTypeCategorie] => 0
            [nomImage] => 1916.png
            [ponderation] => 2
            [fils] => Array
                (
                    [0] => Array
                        (
                            [id] => 1136
                            [libelle] => Fruits
                            [idTypeCategorie] => 1
                            [nomImage] => 1928.png
                            [ponderation] => 1
                            [mea] => 202
                            [fils] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 1139
                                            [libelle] => Pommes & Poires
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 1
                                            [nbProduits] => 5
                                            [fils] => Array
                                                (
                                                )

                                        )

                                    [1] => Array
                                        (
                                            [id] => 1137
                                            [libelle] => Agrumes
                                            [idTypeCategorie] => 2
                                            [nomImage] => 
                                            [ponderation] => 2
                                            [nbProduits] => 3
                                            [fils] => Array
                                                (
                                                )

                                        )


 

    for($i=0; $i< $nb_elem ; $i++){ // while we're not at the end of big array 

    foreach($tab_categories as $key => $objet){//for each element(objet) which has menu and foreach menu we fill database with id and libelle
        $id = $tab_categories[$i] -> id;
        $menu1 = utf8_decode($tab_categories[$i] -> libelle);
        //echo $id."|".$menu1."\n";
        $req="INSERT IGNORE INTO menu_itm (id, menu1)
        VALUES('".$id."','".addslashes($menu1)."');"; //addslashes permet de gérer les caractère spéciaux
                    $req=$bd->prepare($req);
                    $req->execute();

    }
}

我试过这样:

功能pour_un_objet($ OBJ){结果
        的foreach($ OBJ - >对象为$ CLE => $数组){// ...
            如果(is_array($阵列)及和放大器;计数($数组)> 0){//思乐冠军DE L'OBJET柯朗EST联合国画面等SI CE dernier n'est PAS韦迪
                pour_un_fils($数组);
                返回true; //费尔开采
        }
            其他
                返回false; //费尔非利用,

function pour_un_objet($obj) {
foreach($obj ->Object as $cle => $array){//... if(is_array($array) && count($array)>0 ){//si le champ de l'objet courant est un tableau et si ce dernier n'est pas vide pour_un_fils($array); return true; //fils exploitable } else return false; // fils non exploitable,

}

功能pour_un_fils($输入){
    array_walk_recursive($输入功能($输入){//为每个术语是一个表,而我们在表的末尾尽快不是因为我们发现一个标签和一个ID

function pour_un_fils($input){ array_walk_recursive($input, function ($input) {//for each term is a table while we're not at the end of the table as soon as we find a label and an id

有关($ CF = 0; $ CF

for($cf=0;$cf

    foreach($input as $subkey ){// input is an array son subkey is his field (id, libelle ... )
        if($subkey=="libelle" || $subkey=="id") 
            get_menu($id,$term);// only if you arrive on a field or libelle id
            return true;
        }   
        else
            return false;           
  }
 }

}
任何帮助很大AP preciation。

} Any help great appreciation.

推荐答案

它的完成,我找到了另一种方式来找回我想要的数据。

It's done,I found another way to retrieve the data that I wanted.

function RecursiveAddOfProducts($listeSon,$listParents_libelle) {
    $result = array();$Product=array();

    if($listeSon) {

       foreach($listSon as $key => $currentNode {
        if(!empty($currentNode)) { // If the product has a son
          $newParent_libelle = $listParents_libelle; 
           $newParent_libelle[] = utf8_decode($currentNode['libelle']); 
           $newParent_id=$listParents_id;
           $newParent_id[] = $currentNode['id']; 
           $produit=array_merge($newParent_id,$newParent_libelle);
           $menu_niveau=count($Product)-1;
          ProductInsert($Product,$menu_niveau);
          RecursiveAddOfProducts($currentNode['fils'], $newParent_libelle); 
         }
       }
    }

}
    $ listeParents =阵列();

} $listeParents=array();

RecursiveAddOfProducts($tab_categories,$listParents); 

我的插入功能如同它:

My insert function works like it :

function ProductInsert($tab,$menu_niveau){
$menu_list = "";
$column_value = "";
for($i=1;$i<= $menu_level ;$i++){
    if($menu_list != '')
        $menu_list .= ', ';
    $menu_list .= "menu".$i;
    if($column_value != '')
        $column_value .= ', ';
    $column_value.= "'".addslashes($tab[$i])."'";
        //var_dump($column_value);
    }
    $req="INSERT IGNORE INTO menu_itm (id, ".$menu_list.",menu_niveau)
    VALUES(".$tab[0].",".$column_value.",'".$menu_level."')";
    echo $req."</br>";
    sql($req);

}

我想一切要感谢你的帮助,所有这些贡献的人是一个很大的帮助。我学到了很多东西。由于瑞安文森特也为你的帮助,你给你的很多时间,我很感谢你为它。)

I would like all to thank you for your help, all the persons which contributed was of a big help. I learnt a lot. Thanks to Ryan vincent also for your help, you gave many of your time, I am grateful to you for it.;)

这篇关于递归数据库充填多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆