Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
更改数组上的键\u chunk php_Php_Arrays - Fatal编程技术网

更改数组上的键\u chunk php

更改数组上的键\u chunk php,php,arrays,Php,Arrays,我有这样的基于数组的帖子: Array ( [condition] => Array ( [0] => 1 ) [container] => [cleaning] => Y [owner] => Eagletainer [last_cargo] => 1 [vessel] => [insulation] => 1 [tare] => [gross] => [capacity] => [u

我有这样的基于数组的帖子:

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] => 
)
Array
(
[0] => Array
    (
        [ID] => 1
        [CODE_DAMAGE] => 9
    )

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

 )
我的情况是,我想把很多元素数组分块到另一个数组中,以便在数据库中插入批处理。 这是将这些数组分块的php代码:

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

$detail = $this->get_partition($this->input->post(), 17, 2);
结果是:

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

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

)
我的问题是,如何将密钥[0]和[1]更改为另一个密钥,如[ID]和[CODE_]

我希望它们看起来像这样:

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] => 
)
Array
(
[0] => Array
    (
        [ID] => 1
        [CODE_DAMAGE] => 9
    )

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

 )

重新循环阵列并实现如下所示的预期结果:

$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];
}

如果索引已经正确,则可以传递可选的第三个参数:


试试这个,我希望这有帮助。

试试这个:

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

仅举一个例子,将数组添加到此代码中。它将很好地工作

$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);
$main=Array(Array(1,9)、Array(15,3));
$b=数组('ID','CODE_损坏');
$new_array=array();
foreach($main作为$subarray)
{
$new_array[]=array_combine($b,$subarray);
}
回声';打印(新阵列);

重新循环最终结果,并将其生成一个关联数组,这将解决您的问题。。。