简体   繁体   English

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

[英]Creating select option value list from an array

I have an array like this (stored in the $optDir variable): 我有一个这样的数组(存储在$ 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
        )

)

And i have PHP Codes like this : 而且我有这样的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;
    }

When i call the optCatAds function, give output like below: 当我调用optCatAds函数时,给出如下输出:

<option value="5">Fashion</option>
<option value="6">Mobil</option>
<option value="7">Motor</option>

But, I want to made output like below : 但是,我想输出如下:

<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>

Conditions : 条件 :

  1. Fashion, Mobil, Motor <-- parent 时尚,美孚,汽车<-父母

  2. Fashion have child Atasan Pria 时尚有孩子Atasan PRIA

  3. Atasan Pria have child Hem Atasan Pria有孩子下摆

Can someone help me? 有人能帮我吗? Thank to your help. 感谢您的帮助。

This will work fine for you 这对你来说很好

$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>";

Can you try foreach instead of for loop , 您可以尝试用foreach代替for循环,

foreach($optDir as $k=>$value){     
    $opt .= '<option value="'.$value->id.'">'.$sign.$value->title.'</option>';      
}

Replace $optDir[$i][title] with $optDir[$i]['title'] $optDir[$i][title]替换$optDir[$i]['title']

and $optDir[$i][id] with $optDir[$i]['id'] $optDir[$i][id]$optDir[$i]['id']

Here you are making recursive call so you can not get complete 在这里您正在进行递归调用,因此无法完成

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;
    }
<?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();

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM