我想在数据库 mysql 中插入以下日期值作为 json 值,如下例所示:
我希望它们在数据库表行中:
Column static | Column data_1
Row1: 12 | ["1111111111", "2222222222", "3333333333", "4444444444"]
Row2: 34 | ["5555555555", "6666666666", "7777777777"]
Row1: 56 | ["8888888888", "9999999999"]
演示: http ://codepad.viper-7.com/WzGz3p
<form method="post">
<input name="static[]" value="12">
<input name="data_1[]" value="1111111111">
<input name="data_1[]" value="2222222222">
<input name="data_1[]" value="3333333333">
<input name="data_1[]" value="4444444444">
<input name="static[]" value="34">
<input name="data_1[]" value="5555555555">
<input name="data_1[]" value="6666666666">
<input name="data_1[]" value="7777777777">
<input name="static[]" value="56">
<input name="data_1[]" value="8888888888">
<input name="data_1[]" value="9999999999">
<input type="submit">
</form>
我的 php 代码不起作用:
<?php
$data = array();
$data_1 = $_POST['data_1'];
$static = $_POST["static"];
foreach($static as $idx=>$val){
$data[] = array(
'static' => $static[$idx],
'data_1' => json_encode(Array($data_1[$idx*2],$data_1[$idx*2+1]))
);
}
//$this->db->insert_batch('MyTable', $data);
echo "<pre>";
print_r($data);
?>