【问题标题】:How to display the below json data based on the key value"type"如何根据键值“type”显示下面的json数据
【发布时间】:2018-09-05 22:21:43
【问题描述】:

在代码下方找到:

   @foreach($paymentinvoice['items']['item']['0'] as $key => $invoice)
                        @if($key == 'description')
                            <td>{{$invoice}}</td>
                        @endif
                        @if($key == 'amount')
            <td></td>
                            <td><i class="fa fa-inr"></i> {{$invoice}}</td>
                        @endif              

                    @endforeach

我需要显示description的数据,但是接收到的数组是0,1,2,3。如何使用 foreach 循环拆分数据。

找到下面的json数据:

                         [items] => Array
    (
        [item] => Array
            (
                [0] => Array
                    (
                        [id] => 1175
                        [type] => Hosting
                        [relid] => 857
                        [description] => Super Lite - udytfuy.com (04/09/2018 - 03/09/2021)
                        [amount] => 3924.00
                        [taxed] => 1
                    )

                [1] => Array
                    (
                        [id] => 1176
                        [type] => Hosting
                        [relid] => 858
                        [description] => Ultimate - jdsgcsgjcfshg.com (04/09/2018 - 03/09/2021)
                        [amount] => 14004.00
                        [taxed] => 0
                    )

                [2] => Array
                    (
                        [id] => 1177
                        [type] => DomainRegister
                        [relid] => 340
                        [description] => Domain Registration - jdsgcsgjcfshg.com - 1 Year/s (04/09/2018 - 03/09/2019)
                        [amount] => 637.70
                        [taxed] => 1
                    )

                [3] => Array
                    (
                        [id] => 1178
                        [type] => DomainRegister
                        [relid] => 339
                        [description] => Domain Registration - udytfuy.com - 1 Year/s (04/09/2018 - 03/09/2019)
                        [amount] => 637.70
                        [taxed] => 1
                    )

            )

    )

从上面的 json 数据中,我需要根据“类型”显示描述和数量。

【问题讨论】:

  • 你能通过 dd($paymentinvoice) 或 print_r($paymentinvoice) 发布 $paymentinvoice 的转储
  • 如何打印数组 [0],[1],[2],[3],... 中的数据,当我添加更多订单时,数组大小会增加。我上面给出的 json 数据是 $paymentinvoice 的转储。

标签: laravel laravel-5 laravel-4 laravel-5.2


【解决方案1】:

试试这个

@foreach($paymentinvoice['items']['item'] as $invoice)
                        <td>{{$invoice['description']}}</td>
        <td></td>
                        <td><i class="fa fa-inr"></i> {{$invoice['amount']}}</td>

                @endforeach

【讨论】:

    猜你喜欢
    • 2011-04-15
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    • 1970-01-01
    • 2019-03-30
    • 2021-11-11
    • 1970-01-01
    相关资源
    最近更新 更多