这是我的数组:
Array ( [TR] => Array ( [name] => sayfa adı [description] =>
sayfa icerigi
[slug] => sayfa linki ) [EN] => Array ( [name_en] => page name [description_en] =>
page information
[slug_en] => page link ) [DE] => Array ( [name_de] => seite name [description_de] =>
seite informationen
[slug_de] => seite link ) )
这是我的代码初始化器插入代码:
$_INS = array(
'lang_id' => '?',
'name' => '?',
'description' => '?',
'slug' => '?',
'page_id' => $insert_id,
'adding' => $adding,
'updated_at' => date('Y-m-d H:i:s'),
'created_at' => date('Y-m-d H:i:s')
);
我想用新行将每个数组添加到数据库中
答案 0 :(得分:0)
带有键修饰符的简单嵌套的foreach可以删除下划线,并且可以批量插入:
<?php
$array = array(
'TR' => array (
'name' => 'stmtA_1',
'description' => 'stmtA_2',
'slug' => 'stmtA_3',
),
'EN' => array(
'name_en' => 'stmtB_1',
'description_en' => 'stmtB_2',
'slug_en' => 'stmtB_3',
),
);
$insert_id = null;
$adding = null;
$i = 0;
$data = array();
foreach ($array as $lang => $values) {
$data[$i] = array(
'page_id' => $insert_id,
'adding' => $adding,
'updated_at' => date('Y-m-d H:i:s'),
'created_at' => date('Y-m-d H:i:s'),
'lang_id' => $lang
);
foreach ($values as $identifier => $value) {
$key = explode('_', $identifier)[0];
$data[$i][$key] = $value;
}
$i++;
}
echo '<pre>';
print_r($data);
$this->db->insert_batch('my_table', $data);