Php 发送到CodeIgniter的数组存在JSON_解码问题

Php 发送到CodeIgniter的数组存在JSON_解码问题,php,codeigniter,json,Php,Codeigniter,Json,我确信我犯了一个简单的错误。。。但就是找不到 最终,我发布了一个来自Android应用程序的JSON数组(该部分正在运行),但目前我只是在两个PHP页面之间进行测试(1:使用基本表单测试PHP页面,2:CodeIgniter最终目的地),以下是我所拥有的: 在表格页: <form action="bambooinvoice/index.php/api2/newinvoice/4/0/0" method="post"> <?php $array = ar

我确信我犯了一个简单的错误。。。但就是找不到

最终,我发布了一个来自Android应用程序的JSON数组(该部分正在运行),但目前我只是在两个PHP页面之间进行测试(1:使用基本表单测试PHP页面,2:CodeIgniter最终目的地),以下是我所拥有的:

在表格页:

<form action="bambooinvoice/index.php/api2/newinvoice/4/0/0" method="post">
    <?php 
        $array = array("items"=>array(
            "taxable"=>1, 
            "quantity"=>1, 
            "amount"=>123.99, 
            "work_description"=>"this is a test"));
        $json = json_encode($array);
    ?>
    <input type="hidden" name=json value=<?php $json ?> />
    <input type="submit" name="btnSendForm" value="Send" />
</form>
在codeIgniter方面,我有:

$input = $this->input->post('json');
$items = json_decode($input, TRUE);

$amount = 0;
foreach ($items as $item) // In case there are multiple 'items'
{
    $taxable = (isset($item['taxable']) && $item['taxable'] == 1) ? 1 : 0;

    $invoice_items = array(
        'quantity' => $item['quantity'],
        'amount' => $item['amount'],
        'work_description' => $item['work_description'],
        'taxable' => $taxable
    );

    $this->_addInvoiceItem($invoice_items); //simply adding contents to DB
}
最后我收到了错误:(实际上我在所有的调整中都收到了很多错误,但这是一个我似乎无法撼动的错误)

已编辑-更正打字错误。

试试这个
试试这个

将关联数组编码为JSON时,它将成为一个对象,不再是数组。现在,当您解码JSON时,php将创建JSON的对象,而不是关联数组。

当您将关联数组编码为JSON时,它将成为一个对象,不再是数组。现在,当您解码JSON时,php会创建JSON的对象,而不是一个关联数组。

当表单发布名为
JSON
的隐藏值时,您使用的是
$this->input->post('items')

如果您
var\u dump($this->input->post('items'))
,它应该是
FALSE
NULL

请在您的CI脚本中尝试以下操作:

$input = $this->input->post('json'); // not 'items'
$items = json_decode($input, TRUE);

// Rest of your code...

这应该可以解决这个问题,但您还需要确保json数据一开始就被正确发送
var\u dump($\u POST)
应该显示它是否完整地加入到脚本中。

当表单发布名为
json
的隐藏值时,您正在使用
$this->input->POST($items')

如果您
var\u dump($this->input->post('items'))
,它应该是
FALSE
NULL

请在您的CI脚本中尝试以下操作:

$input = $this->input->post('json'); // not 'items'
$items = json_decode($input, TRUE);

// Rest of your code...

这应该可以解决这个问题,但您还需要确保json数据一开始就被正确发送
var\u dump($\u POST)
应该显示它是否能够完整地应用到脚本中。

您可以在数组中更改而不使用json\u编码和序列化

在你的表格中:

<form action="bambooinvoice/index.php/api2/newinvoice/4/0/0" method="post">
    <?php 
        $array = array("items"=>array(
            "taxable"=>1, 
            "quantity"=>1, 
            "amount"=>123.99, 
            "work_description"=>"this is a test"));
        $json = serialize($array);
    ?>
    <input type="hidden" name=json value="<?php echo $json ?>" />
    <input type="submit" name="btnSendForm" value="Send" />
</form>
然后在codeigniter侧,您可以执行以下操作:

<?php
$input = $this->input->post('json');
$items = unserialize($input);

$amount = 0;
foreach ($items as $item) // In case there are multiple 'items'
{
    $taxable = (isset($item['taxable']) && $item['taxable'] == 1) ? 1 : 0;

    $invoice_items = array(
        'quantity' => $item['quantity'],
        'amount' => $item['amount'],
        'work_description' => $item['work_description'],
        'taxable' => $taxable
    );

    $this->_addInvoiceItem($invoice_items); //simply adding contents to DB
}
?>

但是要小心,因为当你使用一个foreach数组时,数组中包含了超过1级的元素。
我希望这对您有所帮助。

您可以在数组中更改而不使用json_编码和序列化

在你的表格中:

<form action="bambooinvoice/index.php/api2/newinvoice/4/0/0" method="post">
    <?php 
        $array = array("items"=>array(
            "taxable"=>1, 
            "quantity"=>1, 
            "amount"=>123.99, 
            "work_description"=>"this is a test"));
        $json = serialize($array);
    ?>
    <input type="hidden" name=json value="<?php echo $json ?>" />
    <input type="submit" name="btnSendForm" value="Send" />
</form>
然后在codeigniter侧,您可以执行以下操作:

<?php
$input = $this->input->post('json');
$items = unserialize($input);

$amount = 0;
foreach ($items as $item) // In case there are multiple 'items'
{
    $taxable = (isset($item['taxable']) && $item['taxable'] == 1) ? 1 : 0;

    $invoice_items = array(
        'quantity' => $item['quantity'],
        'amount' => $item['amount'],
        'work_description' => $item['work_description'],
        'taxable' => $taxable
    );

    $this->_addInvoiceItem($invoice_items); //simply adding contents to DB
}
?>

但是要小心,因为当你使用一个foreach数组时,数组中包含了超过1级的元素。
我希望这对您有所帮助。

您是否尝试过
var_dump()
ing
$items
$json
以确保它们是您期望的?您正在将未经scaped的
json
数据转储到属性中。我假设这会导致
DOM
中出现一些中断,并且POST数据也被中断。如果这里没有输入错误,那么这一行“
是的,谢谢您提醒我,我已经输出了各种变量。变量转储($input);=1我以前见过这个,但我认为这意味着在发布的JSONObject(ie-1“item”)中有“1”关联数组。。。但也许我处理得不正确,它被视为bool?…和var_dump($items)=NULL。这说明我的错误在于表单端的帖子和服务端的解码之间。您是否尝试过
var_dump()
ing
$items
$json
以确保它们是您期望的?您正在将未经筛选的
json
数据转储到属性中。我假设这会导致
DOM
中出现一些中断,并且POST数据也被中断。如果这里没有输入错误,那么这一行“
是的,谢谢您提醒我,我已经输出了各种变量。变量转储($input);=1我以前见过这个,但我认为这意味着在发布的JSONObject(ie-1“item”)中有“1”关联数组。。。但也许我处理得不正确,它被视为bool?…和var_dump($items)=NULL。这告诉我我的错误是在表单端的post和服务端的decode之间。不正确。。请参见此,第二个参数不正确。。看到了吗,第二个参数谢谢,阿扎特。你是对的。。。我需要更正这一行。现在是:谢谢你,阿扎特。你是对的。。。我需要更正这一行。现在是:aaaaarrghh!!!(叹气…)它正在正常工作。谢谢你,我不能告诉你那个愚蠢的错误有多让我生气。是的。。。这类事情可能很难被注意到,因为你从不认为你把“容易的部分”搞砸了。很乐意帮忙!aaaaarrghh!!!(叹气…)它正在正常工作。谢谢你,我不能告诉你那个愚蠢的错误有多让我生气。是的。。。这类事情可能很难被注意到,因为你从不认为你把“容易的部分”搞砸了。很乐意帮忙!