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 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。