php - 如何将php数组读入变量
问题描述
我正在使用 ajax 发布我的 json 数据(到 ci 控制器),然后json_decode
读取我的数据。
这个数据来自表
Array
(
[Detail] => Array
(
[0] => Array
(
[row0] => Array
(
)
)
[1] => Array
(
[row1] => Array
(
[0] => Array
(
[cell0] => 04019
)
[1] => Array
(
[cell1] =>
)
[2] => Array
(
[cell2] => 2.00
)
[3] => Array
(
[cell3] =>
)
[4] => Array
(
[cell4] => 4530000
)
[5] => Array
(
[cell5] =>
)
[6] => Array
(
[cell6] =>
)
)
)
)
)
我想使用 ci 模型读取数据并更新数据库
$data = array(
'Customer' => $this->input->post('Customer'),
'Total' => $this->input->post('Total'),
'Detail' => $this->input->post('Detail')
);
$json = json_decode($data['Detail'],true);
$Sql ="";
$row=0;
foreach ($json as $doc)
{
if ($row !=0)
{
$Sql .= ' Insert Detail' + $doc[0]->cell0 . ' ' . $doc[2]->cell2 . ' ' . $doc[4]->cell4;
}
$row++;
}
print_r($Sql);
但它不工作,
如何读取数据数组并将其保存到Sql
var。谢谢
解决方案
这不是 Codeigniter 的工作方式。
你应该尊重 MVC,有这样的东西:
控制器:
$details = json_decode($data['Detail'],true);
$this->load->model('my_model');
foreach($details as $detail)
{
$this->my_model->add_detail($detail);
}
模型:
function add_detail($detail)
{
$this->db->insert('table_name', $detail);
}
推荐阅读
- docker - Docker 卷挂载:如何在容器中查找路径?
- python - python:如何将多个列表合并为一个?
- python - 如何在 FastAPI 中创建复杂的模式?
- java - 如果变量被更新, volatile 是否强制增量刷新内存
- r - 如果存在 NA,则省略整个列表
- tensorflow2.0 - TensorFlow Lite - 批量推理
- php - 当 slug 不在数据库中时,Laravel route /{slug} 错误
- sql - 在 PSQL 案例语句中使用正则表达式
- node.js - 使用 AWS 不可调用的 Websocket API
- php - 为什么更改我的项目的语言会导致错误“缺少 [Route: cars.show] [URI: {language}/cars/{car}] 所需的参数”?