没有为Foreach循环提供预期的输出

时间:2018-10-16 12:24:26

标签: php foreach

我有两个数组 $ getusers详细信息

Array
(
    [0] => Array
        (
            [users_id] => 2
        )

    [1] => Array
        (
            [users_id] => 3
        )

    [2] => Array
        (
            [users_id] => 4
        )

)

和$ data ['checkbox']

Array
(
    [1] => 1
    [2] => 3
)

我想将此值插入mysql数据库。如此

 $j=1;
        foreach ($getusersdetails as $userstatus):

            $val1['fk_c_id'] = $data['checkbox'][$j];
            $val1['fk_user_id'] = $userstatus['users_id'];
            $val1['status'] = 1;
            $result1 = $this->db->insert('admin_course_details', $val1);
              $j++;
        endforeach;

在这里我没有得到预期的输出。 预期产量

status | fk_c_id | fk_user_id
1             1     2
1             1     3
1             1     4
1             3      2
1             3      3
1             3      4

我的输出

status | fk_c_id | fk_user_id
    1             1     2
    1             1     3
    1             1     4

我想为每个用户分配1和3(复选框值),所以这里我有3个用户。但是这里仅插入1个复选框值。感谢您的帮助。

1 个答案:

答案 0 :(得分:4)

您需要应用两个foreach()

foreach ($data['checkbox'] as $data):
  foreach ($getusersdetails as $userstatus):
      $val1['fk_c_id'] = $data;
      $val1['fk_user_id'] = $userstatus['users_id'];
      $val1['status'] = 1;
      $result1 = $this->db->insert('admin_course_details', $val1);
  endforeach;
endforeach;