【发布时间】:2017-07-26 14:05:44
【问题描述】:
在插入数据库之前,我将这些输入数据存储在数组中。
$regData = array (
'user_type' => $_SESSION['user']->user_type,
'user_id' => $_SESSION['user']->id,
'child_id' => $this->input->post('child_name[]'),
'tc_id' => $this->input->post('tc_id'),
'tc_sources' => $this->input->post('tc_sources'),
'tc_created_date' => date('Y-m-d H:i:s'),
);
$regData 在数组中的输出:
Array
(
[user_type] => parent
[user_id] => 5
[child_id] => Array
(
[0] => 47
[1] => 49
)
[tc_id] => 11
[tc_sources] => Email
[tc_created_date] => 2017-07-26 21:56:57
)
我正在尝试foreach 数组以确保它可以插入到表中的不同行中,如下所示:
foreach ($regData as $k) {
foreach ($k as $val) {
$newArr[] = array(
'user_type' => $val['user_type'],
'user_id' => $val['user_id'],
'child_id' => $val['child_id'],
'tc_id' => $val['tc_id'],
'tc_sources' => $val['tc_sources'],
'tc_created_date' => $val['tc_created_date'],
);
}
}
编辑
这是我想要的 数组形式 的输出,但是数据都具有相同的值,我真的不知道如何在循环后对数组进行 foreach 以获得确切的数据:
Array
(
[0] => Array
(
[user_type] => 4
[user_id] => 4
[child_id] => 4
[tc_id] => 4
[tc_sources] => 4
[tc_created_date] => 4
)
[1] => Array
(
[user_type] => 4
[user_id] => 4
[child_id] => 4
[tc_id] => 4
[tc_sources] => 4
[tc_created_date] => 4
)
)
【问题讨论】:
-
为什么不将数组 vals 连接为 csv 字符串?
-
@ThisGuyHasTwoThumbs 你的意思是喜欢使用
implode吗? -
yar - 如果你这样做,你可以将一个 id 数组更改为 23,24,25,然后在你希望它再次作为数组时在 , 上爆炸
标签: php arrays multidimensional-array foreach