dongzhan5246 2014-07-13 15: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 15: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条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀