简体   繁体   English

如何输出Json数组并遍历变量

[英]How to output a Json Array and loop through variables

I have a small ticket app on my site where users can buy tickets and add guests. 我的网站上有一个小型票务应用程序,用户可以在其中购买票证并添加客人。 I'm sending out the Receipt Email and need to display the guest names. 我正在发送回执电子邮件,需要显示来宾姓名。

Note, There can be up to 7 guests. 请注意,最多可容纳7位客人。

The Guest are in an array. 来宾在数组中。 How can I display the guests properly in the email? 如何在电子邮件中正确显示邀请对象?

Here is what I'm working with. 这是我正在使用的。

Email: 电子邮件:

@php
$array = json_encode($guests);
@endphp

@component('mail::message')
# Greetings,...

## Here is an outline of your transaction:
@component('mail::panel')
...
- Guests: @if(!empty($array)  && isset($array)  && is_array($array))
@foreach ($array as $item)
{{ $item->guest_name  }} | {{ $item->gluten  }}
@endforeach
@endif
@endcomponent

The array at the top, wrapped in the PHP Tags outputs the following data: 顶部的数组(包装在PHP标记中)输出以下数据:

[{"no":1,"guest_name":"John Doe","gluten_free":"Yes"}]

I'm currently not getting any errors to debug, but I'm not getting anything outputted in the email either. 我目前没有任何错误可以调试,但是我也没有在电子邮件中输出任何内容。

I'm using Laravel so here is my Mail File: I tried writting the logic in here as well, but same results. 我正在使用Laravel,所以这是我的邮件文件:我也尝试在此处编写逻辑,但结果相同。

<?php

namespace App\Mail;

use ...

class NewOrder extends Mailable
{
    use Queueable, SerializesModels;
    public ...
    public $guests;

    public function __construct(
            $...
            $guests
    )
    {
            $...
            $this->guests = $guests;
            // $this->guests = json_encode($guests);
    }

    public function build()
    {
        return $this->subject('Order Email')->markdown('emails.user.newOrderEmail');
    }
}

Any help would be appreciated. 任何帮助,将不胜感激。

You need to push all the elements into one master array and then need to pass it mail function. 您需要将所有元素推送到一个主数组中,然后需要将其传递给mail函数。

$details = array();
- Guests: @if(!empty($array)  && isset($array)  && is_array($array))
@foreach ($array as $item)
//{{ $item->guest_name  }} | {{ $item->gluten  }}
 $current_user = array('name'=>$item->guest_name,'gluten'=>$item->gluten);
 array_push($details,$current_user);
@endforeach

Now pass the $details array to mail, I dont know how you do it, but this is how you can pass multiple values store it in array and pass that array. 现在将$details数组传递给邮件,我不知道该怎么做,但这是如何传递多个值并将其存储在数组中并传递该数组的方法。

Mail 邮件

<?php

namespace App\Mail;

use ...

class NewOrder extends Mailable
{
    use Queueable, SerializesModels;
    public ...
    public $guests;

    public function __construct(
            $...
            $guests
    )
    {
            $...
            $this->guests = $guests;
            // $this->guests = json_encode($guests);
    }

    public function build()
    {
        return $this->subject('Order Email')
            ->markdown('emails.user.newOrderEmail')
            ->with([
                'ticket_id' => $this->guests['ticket_id'],
                ....
            ]);
    }
}

Markdown 降价促销

{{$ticket_id}}

If you do json_encode the variable it would become a string. 如果您对变量进行json_encode,它将成为字符串。 You should be able to directly loop over $guests array (assuming it is already an array or a collection, not any other type). 您应该能够直接循环$guests数组(假设它已经是数组或集合,而不是其他任何类型)。
Probably you aren't getting any output as @if(!empty($array) && isset($array) && is_array($array)) condition is false, as $array is a string, not an array. 可能您没有得到任何输出,因为@if(!empty($array) && isset($array) && is_array($array))条件为false,因为$array是字符串,而不是数组。

You could rewrite your code as: 您可以将代码重写为:

@component('mail::message')
# Greetings,...

## Here is an outline of your transaction:
@component('mail::panel')
...
- Guests:
@unless(empty($guests))
@foreach ($guests as $guest)
{{ $guest['guest_name']  }} | {{ $guest['gluten_free'] }}
@endforeach
@endunless

@endcomponent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM