dousi0144 2016-04-02 22:01
浏览 61

如何从PHP中具有关联输入字段的数组生成所有属性组合

I'im trying to display all combinations of attributes of several arrays with associated price input, for user to fill in. After that, input values are to be saved into database.

For this example, I'm using 3 arrays:

[1] => Array
    (
        [8] => Small
        [1] => Round
    )

[2] => Array
    (
        [6] => 2 Layers
        [4] => 1 Layer
    )

[3] => Array
    (
        [10] => no fruit
        [9] => w/ fruit
    )

With the following combination function

function combination($array, $str = '', $valueKeys = '') {
   $current = array_shift($array);

   if(count($array) > 0) {
       foreach($current as $k => $element) {

           $valueKeys .= $k.'+';
           combination($array, $str.' + '.$element, $valueKeys);
       }
   }
   else{
       foreach($current as $k => $element) {
           $valueKeys .= $k;
           echo '<label>'.substr($str, 3).' + '.$element . '</label> = <input name="attrib_price_'.$valueKeys.'" value="" /><br />' . PHP_EOL;
           $valueKeys = '';

       }
   } 
}

HTML Output

<label>Small + 2 Layers + no fruit</label> = <input name="attrib_price_8+6+10" value="" /><br />
<label>Small + 2 Layers + w/ fruit</label> = <input name="attrib_price_9" value="" /><br />
<label>Small + 1 Layer + no fruit</label> = <input name="attrib_price_8+6+4+10" value="" /><br />
<label>Small + 1 Layer + w/ fruit</label> = <input name="attrib_price_9" value="" /><br />
<label>Round + 2 Layers + no fruit</label> = <input name="attrib_price_8+1+6+10" value="" /><br />
<label>Round + 2 Layers + w/ fruit</label> = <input name="attrib_price_9" value="" /><br />
<label>Round + 1 Layer + no fruit</label> = <input name="attrib_price_8+1+6+4+10" value="" /><br />
<label>Round + 1 Layer + w/ fruit</label> = <input name="attrib_price_9" value="" /><br />

The combinations are all showed, but i have a problem with the price input names, they should include the attribute keys but only the first one is correct (8+6+10). How can I tweak the code to accomplish this? Suggestions welcomed. Thanks!

  • 写回答

1条回答 默认 最新

  • douzi1117 2016-04-02 22:11
    关注

    In the else section You clear the $valueKeys that's why only last element is printed in the next iteration Also in the If section you're appending previous iteration's value to $valueKey:

    function combination($array, $str = '', $valueKeys = '') {
       $current = array_shift($array);
    
       if(count($array) > 0) {
           foreach($current as $k => $element) {
               combination($array, $str.' + '.$element, $valueKeys.$k.'+');
           }
       }
       else{
           foreach($current as $k => $element) {
               echo '<label>'.substr($str, 3).' + '.$element . '</label> = <input name="attrib_price_'.$valueKeys.$k.'" value="" /><br />' . PHP_EOL;
           }
       } 
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?