1

I have array based POST like this :

Array
(
[condition] => Array
    (
        [0] => 1
    )

[container] => 
[cleaning] => Y
[owner] => Eagletainer
[last_cargo] => 1
[vessel] => 
[insulation] => 1
[tare] => 
[gross] => 
[capacity] => 
[unit_type] => IMO 1
[date_of_manu] => 
[name_manu] => 
[last25] => 
[cert25] => 
[last5] => 
[cert5] => 
[list2_item_0] => 1
[list2_kondisi_0] => 9
[list3_item_0] => 15
[list3_kondisi_0] => 3
[comments] => 
)

My case is, I want to chunk a lot of those element array into another array for insert_batch in my database. This is the php code to chunk those array:

public function get_partition($array, $p, $c) {
    $partition = array_slice($array, $p);
    array_pop($partition);
    return $chunk = array_chunk($partition, $c);
}

Now, use it,

$detail = $this->get_partition($this->input->post(), 17, 2);

The result is :

Array
(
[0] => Array
    (
        [0] => 1
        [1] => 9
    )

[1] => Array
    (
        [0] => 15
        [1] => 3
    )

)

My question in, how to change the key [0] and [1] into another key like [ID] and [CODE_DAMAGE]

I want them looked like this :

Array
(
[0] => Array
    (
        [ID] => 1
        [CODE_DAMAGE] => 9
    )

[1] => Array
    (
        [ID] => 15
        [CODE_DAMAGE] => 3
    )

 )
1
  • re-loop the final result and make a associated array of it will make your problem solve... Commented Dec 17, 2015 at 4:59

5 Answers 5

1

Re-loop the array and achieve your desired result like this:

$detail = $this->get_partition($this->input->post(), 17, 2);
$new_array = array();
$count = 0;
foreach($detail as $row){
   $new_array[$count]['ID'] = $row[0];
   $new_array[$count++]['CODE_DAMAGE'] = $row[1];
}
Sign up to request clarification or add additional context in comments.

Comments

0

If the indexes were already correct you could pass the optional third parameter: http://php.net/array_chunk

Comments

0
<?php
$array = array(0 => array(0 => 123, 1 => 1234), 1 => array(0 => 123, 1 => 1234));
$updatedArray = array();
foreach ($array as $k => $v) {
    $updatedArray[$k]['ID'] = $v[0];
    $updatedArray[$k]['CODE_DAMAGE'] = $v[1];
}
?>

Try this, I hope this helps.

Comments

0

Try this:

foreach($detail as $key => $value){
    if($key == 0){
        $detail['ID'] = $value;
        unset($detail[$key]);
    }
    if($key == 1){
        $detail['CODE_DAMAGE'] = $value;
        unset($detail[$key]);
    }
}

Comments

0

Just an example add your array to this code..it will work fine

$main = Array(Array(1,9),Array(15,3));
$b = array('ID', 'CODE_DAMAGE');
$new_array = array();
foreach($main as $subarray)
{
    $new_array[] = array_combine($b, $subarray);        
}
echo'<pre>';print_r($new_array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.