使用codeigniter将多维数组值插入数据库

时间:2017-02-03 11:49:28

标签: php arrays codeigniter

array (size=11)
  'reward_title' => 
    array (size=2)
      0 => string 'kishan' (length=6)
      1 => string 'asd' (length=3)
  'amount' => 
    array (size=2)
      0 => string '100' (length=3)
      1 => string '200' (length=3)
  'description' => 
    array (size=2)
      0 => string 'k' (length=1)
      1 => string 'kk' (length=2)
  'estimated_delivery' => 
    array (size=2)
      0 => string '02/02/2017' (length=10)
      1 => string '02/03/2017' (length=10)
  'shipping_details' => 
    array (size=2)
      0 => string '1' (length=1)
      1 => string '2' (length=1)
  'ship_amount_country' => 
    array (size=2)
      0 => string '1' (length=1)
      1 => string '' (length=0)
  'ship_anywhere_world' => 
    array (size=2)
      0 => string '' (length=0)
      1 => string '5' (length=1)
  'limit_avail' => 
    array (size=2)
      0 => string 'on' (length=2)
      1 => string 'on' (length=2)
  'backer_limit' => 
    array (size=2)
      0 => string '2' (length=1)
      1 => string '6' (length=1)
  'avail_from' => 
    array (size=2)
      0 => string '3' (length=1)
      1 => string '7' (length=1)
  'avail_until' => 
    array (size=2)
      0 => string '4' (length=1)
      1 => string '8' (length=1)

数据库表 screenshot

使用codeigniter将多维数组值插入数据库 帮助我。

1 个答案:

答案 0 :(得分:0)

试试这种方式,

这将是您自己的功能代码,

$data = [];
foreach ($array as $k => $v) {
    foreach ($v as $k1 => $v1) {
        $data[$k1][] = [$k => $v1];
    }
}
foreach ($data as $k => $v) {
    $a        = $this->array_2d_to_1d($v);
    $result[] = $a;
}
$this->db->set($result);

$this->db->insert_batch('table', $result);

在该控制器中创建另一个函数

function array_2d_to_1d($input_array)
{
    $output_array = array();
    for ($i = 0; $i < count($input_array); $i++) {
        for ($j = 0; $j < count($input_array[$i]); $j++) {
            $output_array[key($input_array[$i])] = $input_array[$i][key($input_array[$i])];
        }
    }
    return $output_array;
}

我相信这会奏效。