(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数据库,我需要知道每个类别的值/名称/父级。
答案 0 :(得分:0)
我希望这就是你要找的东西。
<?php
$arr = 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',
);
foreach($arr as $k=>$v)
{
//if(!strpos($v,' '))
switch(substr_count($v,' '))
{
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