如何获取数组中的值以及如何使用codeigniter在数据库中插入

问题描述:

Here i have one array, in this array i have one more array name studentAbsentId, now i want insert the studentAbsentId in database, like here studentAbsentId 2 and studentAbsentId 2 is absent so i need to insert the two rows

My updated code print_r($params);

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

    [schoolId] => 2
    [classId] => 1
    [sectionId] => 1
    [studentAbsentDate] => 2017-04-18
    [reg_date] => 2017-04-18 05:20:17
    [created_by] => 1
)

Now i want to take only studentAbsentId,how can do?

expected results

2

3

这里我有一个数组,在这个数组中我还有一个数组名 studentAbsentId code>, 现在我想在数据库中插入 studentAbsentId code>,就像这里 studentAbsentId code> 2和 studentAbsentId code> 2不存在所以我需要插入两行 p >

我的更新代码 print_r($ params); strong> p> blockquote>

 数组 
(
 [studentAbsentId] =>数组
(
 [0] => 2 
 [1] => 3 
)
 
 [schoolId] => 2 
 [  classId] => 1 
 [sectionId] => 1 
 [studentAbsentDate] => 2017-04-18 
 [reg_date] => 2017-04-18 05:20:17 
 [created_by  ] => 1 
)
  code>  pre> 
 
 

现在我只想拿studentAbsentId,该怎么办? p>

预期结果 p> blockquote>

  2 
 
3 
  code>  pre> 
  div>

Try this it helps :)

<?php

$params=array(
        'studentAbsentId' => array
            ( 2,3
            ),
        'studentAbsentDate' => '2017-04-18',
        'reg_date' => '2017-04-18 04:41:21',
        'created_by' => '1',
    );


foreach($params['studentAbsentId'] as $ff)
{
        $atnEntry = array(
        "studentAbsentId" => $ff,
        "studentAbsentDate" =>$params['studentAbsentDate'],
        "morning" => "1"

        );

        $this->db->insert("student_absent_list" , $atnEntry);
}        
        $return = array("status" => "Success" );
        echo json_encode($return);  

?>

Try this

$test = array('studentAbsentId' => array('2','3'),'studentAbsentDate' => '2017-04-18','reg_date' => '2017-04-18 04:41:21','created_by' => '1');

    foreach($test['studentAbsentId'] AS $tet)
    {
        $array_val = array('studentAbsentId' => $tet,'studentAbsentDate' => $test['studentAbsentDate'],'created_by' => $test['created_by']);

        // echo "<pre>";print_r($array_val);

        $this->db->insert('table_name',$array_val);
    }