首页 > 解决方案 > 如何将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);

但它不工作,

如何读取数据数组并将其保存到Sqlvar。谢谢

标签: php

解决方案


这不是 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);
}

阅读Codeigniter 活动记录


推荐阅读