Laravel | foreach中的两个数组 [英] Laravel | Two arrays in foreach

查看:82
本文介绍了Laravel | foreach中的两个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的数组,我想用这两个数组创建一个表.

I have two different arrays and I want to create a table with these two arrays.

但是我不知道如何通过Laravel Blade在一个foreach循环中实现这一目标.

But I don't know how to realize this in just one foreach loop with Laravel Blade.

第一个数组来自外部api,第二个数组是Laravel集合,我使用-toArray()将其转换为一个数组

The first array is from a external api and the second array was a Laravel Collection I converted to an array with - toArray()

您可以看到两个数组都有10个元素,在我的表中,我希望每一行都包含来自两个数组的列.

As you can see both arrays have 10 elements and in my table I want to have each row with columns from both arrays.

array:10 [▼
  0 => {#193 ▼
    +"instanceID": "Ex1"
    +"alias": null
    +"displayName": "Ex1"
    +"instanceHost": "exserver"
    +"startMode": "automatic"
    +"processID": 2960
    +"status": "running"
    +"startStopError": null
  }
  1 => {#194 ▶}
  2 => {#195 ▶}
  3 => {#196 ▶}
  4 => {#197 ▶}
  5 => {#198 ▶}
  6 => {#199 ▶}
  7 => {#200 ▶}
  8 => {#201 ▶}
  9 => {#202 ▶}
]

array:10 [▼
  0 => array:8 [▼
    "id" => 1
    "name" => "Example"
    "street" => "Exstreet 1"
    "postalcode" => 1234
    "city" => "Town"
    "created_at" => "2015-11-05 16:18:02"
    "updated_at" => "2015-11-05 16:53:58"
    "status" => "Silver"
  ]
  1 => array:8 [▶]
  2 => array:8 [▶]
  3 => array:8 [▶]
  4 => array:8 [▶]
  5 => array:8 [▶]
  6 => array:8 [▶]
  7 => array:8 [▶]
  8 => array:8 [▶]
  9 => array:8 [▶]
]

你能帮我吗?

问候 轻弹

推荐答案

如何? array_merge将完成它,但这将向您显示另一个应该很容易理解的选项.

Hows about this? array_merge will get it done, but this will show you another option that should be fairly easy to understand.

<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Street</th>
            <th>Zip</th>
            <th>City</th>
            <th>Created At</th>
            <th>Updated At</th>
            <th>Status</th>
            <th>Instance ID</th>
            <th>Alias</th>
            <th>Display Name</th>
            <th>Instance Host</th>
            <th>Start Mode</th>
            <th>Process ID</th>
            <th>Status</th>
            <th>Start Stop Error</th>
        </tr>
    </thead>
    <tbody>
        @foreach($secondArray as $key => $row)
            <tr>
                <th>{{ $row['id'] }}</th>
                <th>{{ $row['name'] }}</th>
                <th>{{ $row['street'] }}</th>
                <th>{{ $row['postalcode'] }}</th>
                <th>{{ $row['city'] }}</th>
                <th>{{ $row['created_at'] }}</th>
                <th>{{ $row['updated_at'] }}</th>
                <th>{{ $row['status'] }}</th>
                <th>{{ $firstArray[$key]->instanceId }}</th>
                <th>{{ $firstArray[$key]->alias }}</th>
                <th>{{ $firstArray[$key]->displayName }}</th>
                <th>{{ $firstArray[$key]->instanceHost }}</th>
                <th>{{ $firstArray[$key]->startMode }}</th>
                <th>{{ $firstArray[$key]->processID }}</th>
                <th>{{ $firstArray[$key]->status }}</th>
                <th>{{ $firstArray[$key]->startStopError }}</th>
            </tr>
        @endforeach
    </tbody>
</table>

虽然,就我个人而言,我真的很喜欢这个MultipleIterator选项.

Though, personally, I really like this MultipleIterator option.

这篇关于Laravel | foreach中的两个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆