循环 JSON 数组


Loop Json array

>我有这种 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 表格上列出发票

身份证、到期日、状态、付款方式

请帮助如何循环此数组

我试过了

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

但不起作用

你有一个语法错误

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

另外,我认为您的循环

不正确
$array['invoices']['invoice'] 

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

$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>";
}