从数组创建选择选项值列表

时间:2013-12-17 09:31:35

标签: php arrays parent-child

我有一个这样的数组(存储在 $ 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.="&nbsp;";}
            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>

条件:

  1. Fashion,Mobil,Motor&lt; - parent

  2. 时尚有孩子Atasan Pria

  3. Atasan Pria 有孩子下摆

  4. 有人能帮助我吗?谢谢你的帮助。

4 个答案:

答案 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.="&nbsp;";}
        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.="&nbsp;";}
            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();

?>