【问题标题】:PHP how to find array value from string postPHP如何从字符串帖子中查找数组值
【发布时间】:2016-01-13 14:20:30
【问题描述】:

使用 PHP 和 codeigniter。 我在下面发布到我的 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) } }

【问题讨论】:

  • echo $teacherList[0];echo array_values($teacherList); 的输出是什么

标签: php codeigniter


【解决方案1】:

您不必要地将 $data['teacherId'] 包装在一个额外的数组中,而是这样做:

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

具体来说,产生hello x的次数,其中x为上述$teacherList数组中的元素个数:

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

【讨论】:

    猜你喜欢
    • 2016-06-07
    • 2013-10-22
    • 2021-06-02
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    相关资源
    最近更新 更多