dongzhan5246 2014-07-13 07:17
浏览 24
已采纳

PHP foreach基于数组变量

I have the following array and PHP code - this adds an option to a drop down list and works fine but this is one drop down list.

However, what I would like to achieve is for a separate drop down list to be created for each of the unique values in the array [0] in the array.

So based on the array below there would be three drop down lists, one for ACME, one for EDNON and one for VALUE and each list would be populated with information already included in the PHP code below.

Array

Array (
[4f5hfgb] => Array (
[0] => ACME
[1] => 4f5hfgb
[2] => Aberdeen
)
[sdf4ws] => Array (
[0] => ACME
[1] => sdf4ws
[2] => Birmingham 
)
[dfgdfg54] => Array (
[0] => EDNON
[1] => dfgdfg54
[2] => Birmingham 
)
[345bfg] => Array (
[0] => EDNON
[1] => 345bfg
[2] => Birmingham 
)
[345fgfd] => Array (
[0] => VALVE
[1] => 345fgfd
[2] => Birmingham 
)
)

PHP

echo "
    <span class='Question SelectBox'>
    <span class='qnum noshow'></span>
    <span class='qtext'>Option</span>
    <span class='shown'>
     <select class='combobox' id='option' name='option' data-placeholder=\"Option...\" '>
       <option value=\"\">Option...</option>";

    foreach ($tmp as $value) {
     echo "<option name='".$value[0]."' value='".$value[1]."'>".$value[2]."</option>";
    }
     echo"</select>
     </span>
     </span>
     <span class='clearfix'></span>";

Attempted code

I have tried modifying the code but this only produces one drop down and the options are duplicated:

foreach ($tmp as $value) {
    if($value[0]=='ACME'){
    echo "<option name='".$value[0]."' value='".$value[1]."'>".$value[2]."</option>";echo "<option name='".$value[0]."' value='".$value[1]."'>".$value[2]."</option>";
    }

}

I'm struggling to understand if this is actually possible. Any advice, feedback and suggestions welcomed.

展开全部

  • 写回答

2条回答 默认 最新

  • doumie7914 2014-07-13 07:56
    关注

    Something like this

    //------------------------------------------------------------
    // Original array of data
    //------------------------------------------------------------
    $a = array(
    '4f5hfgb'  => array('ACME', '4f5hfgb', 'Aberdeen'),
    'sdf4ws'   => array('ACME', 'sdf4ws', 'Birmingham'),
    'dfgdfg54' => array('EDNON', 'dfgdfg54', 'Birmingham'),
    '345bfg'   => array('EDNON', '345bfg', 'Birmingham'),
    '345fgfd'  => array('VALVE', '345fgfd', 'Birmingham'));
    
    //------------------------------------------------------------
    // Group the options by 'name'
    //------------------------------------------------------------
    $aGroupedOptions = array();
    foreach($a as $aRawOption) {
        list($sName, $sValue, $sOption) = $aRawOption;
        $aGroupedOptions[$sName][]      = array($sValue, $sOption);
    }
    
    //------------------------------------------------------------
    // Build the HTML
    //------------------------------------------------------------
    foreach($aGroupedOptions as $sName => $aOptions) {
        echo
        '<span class="Question SelectBox">
        <span class="qnum noshow"></span>
        <span class="qtext">Option</span>
        <span class="shown">
         <select class="combobox" id="option-' . $sName . '" name="option-' . $sName . '">';
    
        foreach($aOptions as $aOption) {
            list($sValue, $sOption) = $aOption;
            echo '<option name="'. $sName . '" value="' . $sValue . '">' .
                 $sOption . '</option>';
    
         echo"</select>
         </span>
         </span>
         <span class='clearfix'></span>";
    }
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部