我有一个这样的数组(存储在 $ optDir 变量中):
Array
(
[0] => Array
(
[id] => 8
[title] => Atasan Pria
[idparent] => 5
)
[1] => Array
(
[id] => 5
[title] => Fashion
[idparent] => 0
)
[2] => Array
(
[id] => 7
[title] => Motor
[idparent] => 0
)
[3] => Array
(
[id] => 6
[title] => Mobil
[idparent] => 0
)
[4] => Array
(
[id] => 9
[title] => Hem
[idparent] => 8
)
)
我有这样的PHP代码:
function optCatAds($pos=0, $level=0){
global $optDir; $opt = "";
$n = count($optDir);
for($i=0;$i<$n;$i++){
$l = $level*3;
$sign=""; for ($z=0;$z<=$l;$z++){$sign.=" ";}
if($optDir[$i]['idparent']==$pos){
$opt .= '<option value="'.$optDir[$i][id].'">'.$sign.$optDir[$i][title].'</option>';
optCatAds($optDir[$i][id], $level + 1);
}
}
return $opt;
}
当我调用 optCatAds 函数时,请输出如下输出:
<option value="5">Fashion</option>
<option value="6">Mobil</option>
<option value="7">Motor</option>
但是,我想输出如下:
<option value="5">Fashion</option>
<option value="8"> Atasan Pria</option>
<option value="9"> Hem</option>
<option value="6">Mobil</option>
<option value="7">Motor</option>
条件:
Fashion,Mobil,Motor&lt; - parent
时尚有孩子Atasan Pria
Atasan Pria 有孩子下摆
有人能帮助我吗?谢谢你的帮助。
答案 0 :(得分:1)
这对你来说很好用
$optDir = Array(Array("id"=>8,"title"=>"Atasan Pria","idparent"=>5),
Array("id"=>5,"title"=>"Fashion","idparent"=>0),
Array("id"=>7,"title"=>"Motor","idparent"=>0),
Array("id"=>6,"title"=>"Mobil","idparent"=>0),
Array("id"=>9,"title"=>"Hem","idparent"=>8)
);
function optCatAds($pos=0, $level=0)
{
global $optDir;$opt;
for($i=0;$i<count($optDir);$i++)
{
$l = $level*3;
$sign="";
for ($z=0;$z<$l;$z++){$sign.=" ";}
if($optDir[$i]['idparent']==$pos)
{
$opt .= '<option value="'.$optDir[$i]['id'].'">'.$sign.$optDir[$i]['title'].'</option>';
$opt .= optCatAds($optDir[$i]['id'], $level + 1);
}
}
return $opt;
}
$res = optCatAds($pos=0, $level=0);
echo "<select>{$res}</select>";
答案 1 :(得分:0)
您可以尝试foreach
而不是for
循环,
foreach($optDir as $k=>$value){
$opt .= '<option value="'.$value->id.'">'.$sign.$value->title.'</option>';
}
答案 2 :(得分:0)
将$optDir[$i][title]
替换为$optDir[$i]['title']
和$optDir[$i][id]
与$optDir[$i]['id']
在这里,您正在进行递归调用,因此无法完成
function optCatAds($pos=0, $level=0){
global $optDir; $opt = "";
foreach($optDir as $each){
if($each['id']==8) $level = 2;
if($each['id']==9) $level = 3;
$l = $level*3;
$sign=""; for($z=0;$z<=$l;$z++){$sign.=" ";}
if($each['idparent']==$pos){
$opt .= '<option value="'.$each['id'].'">'.$sign.$each['title'].'</option>';
}
}
return $opt;
}
答案 3 :(得分:0)
<?php
$optDir = Array(
Array(
'id' => 8,
'title' => 'Atasan Pria',
'idparent' => 5
),
Array(
'id' => 5,
'title' => 'Fashion',
'idparent' => 0
),
Array(
'id' => 7,
'title' => 'Motor',
'idparent' => 0
),
Array(
'id' => 6,
'title' => 'Mobil',
'idparent' => 0
),
Array(
'id' => 9,
'title' => 'Hem',
'idparent' => 8
)
);
function getOptionsRecursive($idParent = 0, $spaces = '') {
global $optDir;
$s = '';
for($i = 0 ; $i < sizeof($optDir) ; $i++) {
$current = $optDir[$i];
if($current['idparent'] == $idParent) {
$s .= '<option value="'.$current['id'].'">'.$spaces.$current['title'].'</option>';
$s .= getOptionsRecursive($current['id'], $spaces.' ');
}
}
return $s;
};
echo getOptionsRecursive();
?>