PHP如何从字符串post中查找数组值


PHP how to find array value from string post

使用PHP;代码点火器。我在下面发布到我的PHP api从前端提交->{"academicYear":"2015","subjectClassification":"Primary","subjectId":"55","teacherId":[10,16,17]}

我需要在PHP代码中查找或打印teacherId值。基本上,如果teacherId数组中有3个Id,我的目标是打印"HELLO"3次

我的代码如下,

function subjectTeacherAllocation_post(){
        $data = remove_unknown_fields($this->post() ,$this->form_validation->get_field_names('subjectTeacherAllocation_post'));
        $this->form_validation->set_data($data);
        var_dump($data);
        $teacherList = array($data['teacherId']);
        echo $teacherList[0];
        echo array_values($teacherList);

var_dump输出-->array(3) { ["academicYear"]=> string(4) "2014" ["subjectId"]=> string(2) "55" ["teacherId"]=> array(3) { [0]=> int(9) [1]=> int(15) [2]=> int(32) } }

您不必要地将$data['teacherId']封装在一个额外的数组中,而只需执行:

$teacherList = $data['teacherId'];
echo $teacherList[0]; //9

具体来说,生成hello x次数,其中x是上述$teacherList数组中的元素数量:

foreach($teacherList as $unused){
    echo 'hello';
}