【问题标题】:Loop Json array循环Json数组
【发布时间】:2015-01-27 05:11:24
【问题描述】:

我有这种json数组

[result] => success
    [totalresults] => 20
    [startnumber] => 0
    [numreturned] => 20
    [invoices] => Array
        (
            [invoice] => Array
                (
                    [0] => Array
                        (
                            [id] => 2014088828
                            [userid] => 1
                            [firstname] => Rony
                            [lastname] => Raymaekers
                            [companyname] => Raymaekers Rony
                            [invoicenum] => 
                            [date] => 2014-11-19
                            [duedate] => 2014-12-03
                            [datepaid] => 0000-00-00 00:00:00
                            [subtotal] => 49.59
                            [credit] => 0.00
                            [tax] => 10.41
                            [tax2] => 0.00
                            [total] => 60.00
                            [taxrate] => 21.00
                            [taxrate2] => 0.00
                            [status] => Unpaid
                            [paymentmethod] => paypal
                            [notes] => 
                            [currencycode] => EUR
                            [currencyprefix] => €
                            [currencysuffix] => EUR
                        )

我想在 html 表格中列出发票

id、到期日、状态、付款方式

请帮助如何循环这个数组

我试过了

$invoice = json_decode($invoices);
    foreach($invoices as $keys) {
         foreach($keys as $invoice => $kom) {
           echo $kom['id'];

      }

      }

但不起作用

【问题讨论】:

  • 你能提供实际的JSON吗?

标签: php arrays json foreach


【解决方案1】:

你有一个语法错误

foreach ($array as $key => $value)

我也不认为你的循环是正确的

$array['invoices']['invoice'] 

从代码的外观来看,应该是 foreach 中的数组。

【讨论】:

    【解决方案2】:
    $data = json_decode($invoices);
    
    foreach($data['invoices'] as $invoice){
       echo "<tr>";
       echo "<td>{$invoice['id']}</td>";
       echo "<td>{$invoice['user_id']}</td>";
       echo "<td>{$invoice['firstname']}</td>";
       echo "<td>{$invoice['lastname']}</td>";
       // and so on
       echo "</tr>";
    }
    

    【讨论】:

    • @lainard 你能试试 var_dump(json_decode($invoices));并给我结果
    猜你喜欢
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 2012-09-16
    • 2013-08-16
    • 2012-10-14
    • 2014-05-18
    相关资源
    最近更新 更多