我有這些輸入數據,我插入到數據庫之前存儲在數組中。Foreach在php中的多維數組
$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
)
)
爲什麼不將concat數組vals設爲csv字符串? – ThisGuyHasTwoThumbs
@ThisGuyHasTwoThumbs你喜歡用'implode'嗎? –
yar - 如果你這樣做,你可以改變一個ID數組爲23,24,25然後爆炸,當你想要它作爲一個數組再次 – ThisGuyHasTwoThumbs