douye1876 2018-04-16 06:47
浏览 15
已采纳

PHP - 如何循环下划线格式化的帖子数据?

I am currently working on a application where I need to parse the data posted into the server. The data is a underscore-delimited string which indicates which parent are they under.

The data posted to the server looks like this:

department: 'Sales'   
s1: 'Logistics'
s1_q1_type: multiple
s1_q1: 'Q 1'
s1_q1_c1: ''
s1_q1_c2: ''
s1_q1_c3: ''
s1_q1_correct_c1: 'on'
s1_q1_correct_c2: 'on'
s1_q1_points: 10
s1_q2_type: multiple
s1_q2: 'Q 2'
s1_q2_c1: ''
s1_q2_c2: ''
s1_q2_points: 10
s2: 'Analysis'
s2_q1_type: multiple
s2_q1: 'Q 1'
s2_q1_c1: ''
s2_q1_c2: ''
s2_q1_correct_c2: 'on'
s2_q1_points: 5
s2_q2_type: multiple
s2_q2: 'Q 2'
s2_q2_c1: ''
s2_q2_c2: ''
s2_q2_points: 15

From the code above: s## are "sections", under sections there are q## (questions), then under questions there is: type, points, c## (choices) and correct answer(s). There can be multiple correct answers per question.

I need to parse the above code to an array that looks like this:

department: 'Sales',
sections: { 
    s1: {
        title: 'Logistics',
        questions: {
            q1: {
                title: 'Q 1',
                type: multiple,
                choices: {
                    c1: '',
                    c2: '',
                    c3: ''
                },
                correct: {
                    c1: 'on', 
                    c2: 'on'
                },
                points: 10
            }, 
            q2: {
                title: 'Q 2', 
                type: multiple, 
                    c1: '',
                    c2: ''
                points: 10
            }
        }
    },

    s2: {
        title: 'Analysis',
        questions: {
            q1: {
                title: 'Q 1',
                type: multiple,
                choices: {
                    c1: '',
                    c2: '',
                    c3: ''
                }
                correct: {
                    c1: 'on'
                },
                points: 5
            }, 
            q2: {
                title: 'Q 2'
                type: multiple,
                    c1: '',
                    c2: ''
                points: 15
            }
        }
    }
}

I tried using foreach and checking if weather the key is isset() but then I'm not getting anywhere.

$i = 1;
$array = array();
foreach( $_POST as $key => $value ){
    if( isset( $post['s' . $i] ) ){
        $array['sections'][$key] = $value;
    }

    $i++;
}

Any help would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • dongyu9263 2018-04-16 07:27
    关注

    I guess this is what you looking for..

    this is just roughly code.. you can take a look

    $everyinput = [
        's1'=>'Logistics',
        's1_q1_type'=>'multiple',
        's1_q1'=>'Q 1',
        's1_q1_c1'=>'',
        's1_q1_c2'=>'',
        's1_q1_c3'=>'',
        's1_q1_correct_c1'=> 'on',
        's1_q1_correct_c2'=> 'on',
    
        's1_q1_points'=>'10',
        's1_q2_type'=>'multiple',
        's1_q2'=>'Q 2',
        's1_q2_c1'=>'',
        's1_q2_c2'=>'',
        's2_q1_correct_c2' =>'on',
        's1_q2_points'=>10,
    
        's2'=>'Analysis',
        's2_q1_type'=>'multiple',
        's2_q1'=>'Q 1',
        's2_q1_c1'=>'',
        's2_q1_c2'=>'',
        's2_q1_points'=>5,
        's2_q2_type'=>'multiple',
        's2_q2'=>'Q 2',
        's2_q2_c1'=>'',
        's2_q2_c2'=>'',
        's2_q2_points'=>15
    ];
    
    $head = [];
    foreach($everyinput as $key => $input) {
        if ($key[0] == 's') {
            if (strpos($key, "_") !== false) {
                $factor = explode("_", $key);
                if (count($factor) > 2) {
                    if ($factor[2] == "type" || $factor[2] == "points") {
                        $head['sections'][$factor[0]]['question'][$factor[1]][$factor[2]] = $input;
                    } else if($factor[2] == 'correct') {
                        $head['sections'][$factor[0]]['question'][$factor[1]]['correct'][$factor[3]] = $input;
                    } else {
                        $head['sections'][$factor[0]]['question'][$factor[1]]['choices'][$factor[2]] = $input;
                    }
                } else {
                    $head['sections'][$factor[0]]['question'][$factor[1]]['title'] = $input;
                }
            } else {
                $head['sections'][$key]['title'] = $input;
            }
        } else {
            $head[$key] = $input;
        }
    }
    
    dd($head);
    

    Result enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名