根据空格字符数确定父级

时间:2013-10-14 02:54:15

标签: php arrays

(PHP) 我有这个数组:

Array
(
    [101] => Category Level One
    [112] =>    Second Name Category
    [173] =>       Suzete,Biberoane
    [177] =>       Carucioare
    [178] =>       Centre Activitati, Carusele
    [180] =>       Scaune Auto Accesorii
    [182] =>       Mobilier Bebelusi
    [113] =>    Jucarii de Exterior
    [203] =>       Leagane, ToboganeBalansoare
    [119] =>    Jucarii Educative
    [147] =>    Vehicule, BicicleteTriciclete
    [272] =>       Biciclete Triciclete
    [274] =>       Vehicule Diverse
    [102] => Category Level One Blabla
    [115] =>    Another Second Category
    [276] =>       Laptopuri
    [277] =>       Tablete
    [278] =>       Accesorii Laptopuri
    [281] =>          Genti Laptopuri
    [282] =>          Memorii Laptopuri
    [283] =>          Coolere Laptopuri
    [279] =>       Accesorii Tablete
    [116] =>    Yet Another Second Categ
    [287] =>       Desktop PC
    [288] =>       Monitoare
    [117] =>    Componente & Periferice
    [289] =>       Componente PC
)

现在这是一个包含父母和孩子的类别列表。 如何根据空格数/。

判断一个节点的父节点

我想将它转换为mysql数据库,我需要知道每个类别的值/名称/父级。

1 个答案:

答案 0 :(得分:0)

我希望这就是你要找的东西。

<?php
$arr = array
    (
    101 => 'Category Level One',
    112 => '&nbsp;&nbsp;&nbsp;Second Name Category',
    173 => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Suzete,Biberoane',
    177 => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Carucioare',
    178 => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Centre Activitati, Carusele',
    180 => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Scaune Auto Accesorii',
    182 => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Mobilier Bebelusi',
    113 => '&nbsp;&nbsp;&nbsp;Jucarii de Exterior',
    203 => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leagane, ToboganeBalansoare',
    119 => '&nbsp;&nbsp;&nbsp;Jucarii Educative',
    147 => '&nbsp;&nbsp;&nbsp;Vehicule, BicicleteTriciclete',
    272 => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Biciclete Triciclete',
    274 => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Vehicule Diverse',
    102 => 'Category Level One Blabla',
    115 => '&nbsp;&nbsp;&nbsp;Another Second Category',
    276 => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Laptopuri',
    277 => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tablete',
    278 => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Accesorii Laptopuri',
    281 => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Genti Laptopuri',
    282 => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Memorii Laptopuri',
    283 => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Coolere Laptopuri',
    279 => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Accesorii Tablete',
    116 => '&nbsp;&nbsp;&nbsp;Yet Another Second Categ',
    287 => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Desktop PC',
    288 => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Monitoare',
    117 => '&nbsp;&nbsp;&nbsp;Componente &amp; Periferice',
    289 => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Componente PC',
);

foreach($arr as $k=>$v)
{
    //if(!strpos($v,'&nbsp;'))

    switch(substr_count($v,'&nbsp;'))
    {
        case 0:
            echo "<br>Parent Node found at $k : $v";
            break;

        case 3:
            echo "<br>Child Found at $k : $v";

            break;

        case 6:
            echo "<br>Sub-Child Found at $k : $v";

            break;
    }

}

输出:

Parent Node found at 101 : Category Level One
Child Found at 112 :    Second Name Category
Sub-Child Found at 173 :       Suzete,Biberoane
Sub-Child Found at 177 :       Carucioare
Sub-Child Found at 178 :       Centre Activitati, Carusele
Sub-Child Found at 180 :       Scaune Auto Accesorii
Sub-Child Found at 182 :       Mobilier Bebelusi
Child Found at 113 :    Jucarii de Exterior
Sub-Child Found at 203 :       Leagane, ToboganeBalansoare
Child Found at 119 :    Jucarii Educative
Child Found at 147 :    Vehicule, BicicleteTriciclete
Sub-Child Found at 272 :       Biciclete Triciclete
Sub-Child Found at 274 :       Vehicule Diverse
Parent Node found at 102 : Category Level One Blabla
Child Found at 115 :    Another Second Category
Sub-Child Found at 276 :       Laptopuri
Sub-Child Found at 277 :       Tablete
Sub-Child Found at 278 :       Accesorii Laptopuri
Sub-Child Found at 279 :       Accesorii Tablete
Child Found at 116 :    Yet Another Second Categ
Sub-Child Found at 287 :       Desktop PC
Sub-Child Found at 288 :       Monitoare
Child Found at 117 :    Componente & Periferice
Sub-Child Found at 289 :       Componente PC