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

时间:2018-04-16 06:47:18

标签: php arrays

我目前正在开发一个应用程序,我需要解析发布到服务器中的数据。数据是以下划线分隔的字符串,表示它们所在的父项。

发布到服务器的数据如下所示:

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
            }
        }
    }
}

从上面的代码:s ##是"部分",在部分下面有q ##(问题),然后在问题下有:类型,点数,c ##(选择)和正确答案(S)。每个问题可以有多个正确答案。

我需要将上面的代码解析为一个如下所示的数组:

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

    $i++;
}

我尝试使用foreach并检查天气是否密钥是isset()然后我没有到达任何地方。

W0411 03:07:37.393124 1 clusterstate.go:514] Failed to get nodegroup for dev-k8s-node-asg-230-i-089e4d2f163533989: Wrong id: expected format aws:////, got
W0411 03:07:37.393145 1 clusterstate.go:514] Failed to get nodegroup for stg-k8s-w2-npe-master-3: Wrong id: expected format aws:////, got
W0411 03:07:37.393152 1 clusterstate.go:514] Failed to get nodegroup for dev-k8s-node-prm-1: Wrong id: expected format aws:////, got
W0411 03:07:37.393158 1 clusterstate.go:514] Failed to get nodegroup for dev-k8s-node-asg-230-i-0eb3341fce85be39c: Wrong id: expected format aws:////, got
W0411 03:07:37.393164 1 clusterstate.go:514] Failed to get nodegroup for dev-k8s-node-asg-230-i-091d1a037311d5daf: Wrong id: expected format aws:////, got
W0411 03:07:37.393169 1 clusterstate.go:514] Failed to get nodegroup for dev-k8s-node-asg-230-i-041dd54f2baaa4553: Wrong id: expected format aws:////, got
W0411 03:07:37.393188 1 clusterstate.go:560] Readiness for node group dev-k8s-node-asg-230 not found
W0411 03:07:37.393203 1 clusterstate.go:560] Readiness for node group stg-k8s-agent-w2-asg not found

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:3)

我想这就是你要找的......

这只是粗略的代码..你可以看看

$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);

结果 enter image description here