【问题标题】:How to display nested array data in laravel blade?如何在 laravel 刀片中显示嵌套数组数据?
【发布时间】:2021-01-20 05:08:48
【问题描述】:

如何访问 laravel 刀片文件中的嵌套数组数据?

我的数组:

{
      "id": 2271,
      "amt_item": "PS839137",
      "image_name": "PS839137.jpg",
      "company": "01",
      "division": "PAP",
      "color_description": "VINTAGE SAGE",
      "item_description": "SHT SLV CRISSCROSS TEE",
      "season_code": "SP18",
      "season_name": null,
      "season_description": null,
      "wholesale_price": ".00",
      "retail_price": ".00",
      "color_code": "VINSA",
      "vendor_code": "DDJJG",
      "vendor_desc": "JIANGYIN CITY JINGE GARMENT COMPANY",
      "color_size_grid": {
        "CORA": {
          "1": {
            "size_description": "X SMALL",
            "on_hand": "0"
          },
          "3": {
            "size_description": "MEDIUM",
            "on_hand": "0"
          },
          "5": {
            "size_description": "X LARGE",
            "on_hand": "0"
          },
          "4": {
            "size_description": "LARGE",
            "on_hand": "0"
          },
          "2": {
            "size_description": "SMALL",
            "on_hand": "0"
          }
        },
        "CHA": {
          "3": {
            "size_description": "MEDIUM",
            "on_hand": "0"
          },
          "4": {
            "size_description": "LARGE",
            "on_hand": "0"
          },
          "1": {
            "size_description": "X SMALL",
            "on_hand": "0"
          },
          "5": {
            "size_description": "X LARGE",
            "on_hand": "0"
          },
          "2": {
            "size_description": "SMALL",
            "on_hand": "0"
          }
        },
        "VINSA": {
          "5": {
            "size_description": "X LARGE",
            "on_hand": "0"
          },
          "3": {
            "size_description": "MEDIUM",
            "on_hand": "0"
          },
          "4": {
            "size_description": "LARGE",
            "on_hand": "0"
          },
          "2": {
            "size_description": "SMALL",
            "on_hand": "0"
          },
          "1": {
            "size_description": "X SMALL",
            "on_hand": "0"
          }
        },
        "VINDE": {
          "5": {
            "size_description": "X LARGE",
            "on_hand": "0"
          },
          "4": {
            "size_description": "LARGE",
            "on_hand": "0"
          },
          "1": {
            "size_description": "X SMALL",
            "on_hand": "0"
          },
          "3": {
            "size_description": "MEDIUM",
            "on_hand": "0"
          },
          "2": {
            "size_description": "SMALL",
            "on_hand": "0"
          }
        }
      }
    }

我将此数组作为 items 传递给我的 laravel Blade 文件并访问相关数据。但是我很难访问 color_size_grid 数组中的数据。如何在 foreach 循环中打印所有 size_description?提前致谢。

在我的刀片文件中做了什么:

@foreach ($items as $item)
        <div class="print-block" style="page-break-inside: avoid;">
            <div class="print-block__img">
                <img src="" alt="">
            </div>
            <div class="print-block__details">
                <h3>{{$item['item_description']}}</h3>
                <div class="code">{{$item['amt_item']}}</div>
                <div class="price">
                    <strong>W:</strong> USD {{$item['wholesale_price']}} | <strong>R:</strong> USD {{$item['retail_price']}}
                </div>
                <table class="other-info">
                    <tr>
                        <td>
                            <strong>Sizes: </strong>
                        </td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>
                            <strong>Colors: </strong>
                        </td>
                        <td></td>
                    </tr>
                </table>
            </div>
        </div>
    @endforeach

对于 Sizes,现在我想打印 color_size_grid 数组中的所有 size_descriptions

每种颜色的可用尺寸如下:

Sizes:
CORA - X SMALL, MEDIUM, X LARGE, LARGE, SMALL
CHA - ...........

【问题讨论】:

    标签: arrays laravel loops nested laravel-blade


    【解决方案1】:
    @foreach ($items as $item)
        <div class="print-block" style="page-break-inside: avoid;">
            <div class="print-block__img">
                <img src="" alt="">
            </div>
            <div class="print-block__details">
                <h3>{{ $item['item_description'] }}</h3>
                <div class="code">{{ $item['amt_item'] }}</div>
                <div class="price">
                    <strong>W:</strong> USD {{ $item['wholesale_price'] }} | <strong>R:</strong> USD {{ $item['retail_price'] }}
                </div>
                <table class="other-info">
                    <tr>
                        <td>
                            <strong>
                                Sizes: 
                                <?php
                                    $uniqueSizes = [];
                                ?>
                                @foreach ($item['color_size_grid'] as $color => $sizes)
                                    @foreach ($sizes as $size)
                                        @if (isset($uniqueSizes[$size['size_description']]))
                                            @continue
                                        @endif
                                        {{ $size['size_description'] }}
                                        {{ ($loop->last ? '' : ',') }}
                                        <?php
                                            $uniqueSizes[$size['size_description']] = true;
                                        ?>
                                    @endforeach
                                @endforeach
                            </strong>
                        </td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>
                            <strong>
                                Colors: 
                                @foreach ($item['color_size_grid'] as $color => $sizes)
                                    {{ $color }}: 
                                    @foreach ($sizes as $size)
                                        {{ $size['size_description'] }}
                                        {{ ($loop->last ? '' : ',') }}
                                    @endforeach
                                @endforeach
                             </strong>
                        </td>
                        <td></td>
                    </tr>
                </table>
            </div>
        </div>
    @endforeach
    

    【讨论】:

    • 感谢您的回答。但我在这里得到一个错误,“试图获取非对象的属性'color_size_grid'
    • 作为 laravel 的新手,能否给我举个例子
    • 我在答案里改了
    • 是的,现在可以使用了,非常感谢 :),我可以修改它以显示每种颜色的尺寸吗?
    • 太好了,是的,你可以修改任何你想要的 ;-)
    猜你喜欢
    • 2020-04-10
    • 2015-03-17
    • 1970-01-01
    • 2021-02-14
    • 2023-02-06
    • 1970-01-01
    • 2018-06-22
    • 2020-10-10
    • 2019-02-22
    相关资源
    最近更新 更多