【问题标题】:Store input field value with select option if product width=22 and select option select product width = inch store like product width = 22 inch如果产品宽度 = 22,则使用选择选项存储输入字段值并选择选项选择产品宽度 = 英寸存储产品宽度 = 22 英寸
【发布时间】:2021-06-20 00:35:02
【问题描述】:

首先我像这样将数据发送到控制器

[Product_Id] => 11
    [selected_Product_Id] => ASIN
    [Product_Name] => 22
    [Brand_Name] => 33
    [Country_Of_Origin] => India
    [Product_Length] => 44
    [selected_Product_Length] => Angstrom
    [Product_Width] => 55
    [selected_Product_Width] => Angstrom
    [Product_Height] => 66
    [selected_Product_Height] => Angstrom
    [Number_of_Pieces] => 77
    [Included_Components] => 88
    [Assembly_Instruction] => 99
    [Size] => 10
    [Wood_Type] => 20

然后在后端存储我使用的数据

foreach($_POST as $key => $value){
        $name = str_replace('_', ' ', $key);
        $replace_name = str_replace('_', ' ', $key) ."=".$value. '<br />' ;
        
        echo $replace_name.<br />;

        //if(strpos($name, 'selected') !== false){

            //$select_name = str_replace('selected', '', $name);

            //echo $select_name.'='.$value.'<br />';

            
        //}else{
            //echo $replace_name;
        //}

    
        // if (preg_match('/\b'.$select_name.'\b/', $name)) {
        //      echo $name ;

        //  }

    }

我得到的数据在这里


产品 ID=11
选择的产品 ID=ASIN
产品名称=22
品牌名称=33
原产国=印度
产品长度=44
选择的产品长度=埃
产品宽度=55
选择的产品宽度=埃
产品高度=66
选择的产品高度=埃
件数=77
包含的组件=88
组装说明=99
尺寸=10
木型=20

但我想要这样

产品 ID=11 ASIN
产品名称=22
品牌名称=33
原产国=印度
产品长度=44 埃
产品宽度=55 埃
产品高度=66 埃
件数=77
包含的组件=88
组装说明=99
尺寸=10
木材类型=20

对不起,我的简短解释我不知道如何解释,但如果我有一个带有选择标签的输入,我希望输入名称为例如产品宽度 = 22 + 选择我在后英寸中获得的标签选项值,如产品宽度 = 22 英寸

当我得到上述数据后,需要将数据转换成这样的数组

array(
    [Product_Id] => 11 ASIN
    [Product_Name] => 22
    [Brand_Name] => 33
    [Country_Of_Origin] => India
    [Product_Length] => 44  Angstrom
    [Product_Width] => 55 Angstrom
    [Product_Height] => 66 Angstrom
    [Number_of_Pieces] => 77
    [Included_Components] => 88
    [Assembly_Instruction] => 99
    [Size] => 10
    [Wood_Type] => 20
)

【问题讨论】:

    标签: php


    【解决方案1】:

    在第一个循环中,您格式化您的密钥并收集“selected_”密钥以便稍后处理(因为它必须附加到值的末尾)

    然后循环这些“selected_”键并附加到现有值。

    最后你把所有的东西都打印出来了:

    <?php
    
    $input = [
        'Product_Id' => 11,
        'selected_Product_Id' => 'ASIN',
        'Product_Name' => 22,
        'Brand_Name' => 33,
        'Country_Of_Origin' => 'India',
        'Product_Length' => 44,
        'selected_Product_Length' => 'Angstrom',
        'Product_Width' => 55,
        'selected_Product_Width' => 'Angstrom',
        'Product_Height' => 66,
        'selected_Product_Height' => 'Angstrom',
        'Number_of_Pieces' => 77,
        'Included_Components' => 88,
        'Assembly_Instruction' => 99,
        'Size' => 10,
        'Wood_Type' => 20,
    ];
    
    $specialKeys = [];
    $formatted = [];
    
    foreach($input as $key => $value) {
        if (strpos($key, 'selected_') === 0) {
            $name = str_replace('selected_', '', $key);
            $name = str_replace('_', ' ', $name);
            $specialKeys[$name] = $value;
    
            continue;
        }
    
        $name = str_replace('_', ' ', $key);
        $replaceName = str_replace('_', ' ', $key) ."=".$value. '<br />' ;
            
        $formatted[$name] = $value;
    }
    
    foreach ($specialKeys as $key => $value) {
        $formatted[$key] .= " {$value}";   
    }
    
    foreach ($formatted as $key => $value) {
        echo "{$key} => $value" . PHP_EOL;
    }
    

    PHPSandbox

    【讨论】:

    • 非常感谢!
    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    • 1970-01-01
    • 2013-06-06
    • 2019-02-26
    相关资源
    最近更新 更多