发布到CodeIgniter的数组的JSON_decode问题

时间:2011-02-28 22:46:37

标签: php codeigniter json

我确信我犯了一个简单的错误......但根本找不到它。

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

在表单页面:

<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>

这创造了(对我来说很好看):

{"items":{"taxable":1,"Quantity":1,"amount":123.99,"work_description":"this is a test"}}

在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
}

最后我收到错误:(我在所有调整中都收到了很多错误,但这是我似乎无法动摇的那个)

Message: Invalid argument supplied for foreach()

编辑 - 纠正错字。

4 个答案:

答案 0 :(得分:3)

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

如果您var_dump($this->input->post('items')),则应为FALSENULL

请在CI脚本中尝试此操作:

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

// Rest of your code...

这应该解决这个问题,但你还需要确保正确发送你的json数据! var_dump($_POST)应该向您显示它是否以单件形式显示在您的脚本中。

答案 1 :(得分:0)

试试这个 <input type="hidden" name=json value='<?=$json?>' /> 或者这个 <input type="hidden" name=json value='<?=str_replace('\'', '&#039;', $json)?>' />
htmlspecialchars

答案 2 :(得分:0)

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

答案 3 :(得分:0)

您可以更改并不使用json_encode并在数组中使用序列化。

在您的表单中这样:

<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>

此序列化函数将创建此输出:

a:1:{s:5:"items";a:4:{s:7:"taxable";i:1;s:8:"quantity";i:1;s:6:"amount";d:123.9899999999999948840923025272786617279052734375;s:16:"work_description";s:14:"this is a test";}}

然后在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
}
?>

但要小心,因为当你使用一个包含更多1级数组的foreach时。 我希望这对你有所帮助。