使用codeigniter更新多行

I am trying to update multiple rows at a time in codeigniter. I have the following a an example of the fields I need to update for each user

<div class="row">
    <input type="text" name="id[46][firstname]" value="">
    <input type="text" name="id[46][lastname]" value="">
</div>

<div class="row">
    <input type="text" name="id[48][firstname]" value="">
    <input type="text" name="id[48][lastname]" value="">
</div>

I am struggling to grab the values for update. The actual update statement shouldn't be a problem, I'm just concerned with retrieving the correct form values per user.

    foreach($_POST as $key => $value){
        $keyparts = explode('_', $key);
        if(count($keyparts) == 2){
            switch ($keyparts[0]) {
                case 'firstname':
                    $records[$keyparts[1]]->firstname = $value;
                    break;
                case 'lastname':
                    $records[$keyparts[1]]->lastname = $value;
                    break;
                default:
                    break;
            }
        }
        // update statement to go here
    }

HTML Code

<div class="row">
<input type="text" name="46[firstname]" value="">
<input type="text" name="46[lastname]"  value="">
</div>

<div class="row">
<input type="text" name="48[firstname]" value="">
<input type="text" name="48[lastname]" value="">
</div>

PHP Code

$records = array();
foreach($_POST as $key => $value){

    if(is_array($value))
    {
        foreach($value as $new_key=>$new_val)
        {
            $records[$key][$new_key] = $new_val;
        }           
    }
}

The problem is that you are not understanding how your POST is building the array. From you HTML above, the form will build the following POST array:

array =>
    [id] =>
        [46] =>
            [firstname] => "john"
            [lastname] => "doe"

        [48] =>
            [firstname] => "jane"
            [lastname] => "doet"

with the array being visually laid out like that, you can now iterate, to obtain your values. However, one thing I do not understand in your code, is why you are using explode(), what are you trying to turn into an array? You can easily iterate and update this information in your database like so:

$this->load->model("my_model");
$postdata = $this->input->post();
foreach($postdata['id'] as $id => $user_info){
     $this->my_model->update_user($id, $user_info); // your model method should handle the update information by ID.
}

if you do echo "<pre>" .print_r($_POST, true)."</pre>"; on the form processing page, the result should look linke this

    Array
(
    [id] => Array
        (
            [46] => Array
                (
                    [firstname] => firstNameValue1
                    [lastname] => lastNameValue1
                )

            [48] => Array
                (
                    [firstname] => firstNameValue2
                    [lastname] => lastNameValue2
                )

        )

)

So, this should help you catch the values:

foreach($_POST['id'] as $key => $value){
  $records[$key] = (object)array();
  $records[$key]->firstname = $value['firstname'];
  $records[$key]->lastname = $value['lastname'];

    // update statement to go here
}

Results this structure:

Array
(
    [46] => stdClass Object
        (
            [firstname] => firstNameValue1
            [lastname] => lastNameValue1
        )

    [48] => stdClass Object
        (
            [firstname] => firstNameValue2
            [lastname] => lastNameValue2
        )

)