简体   繁体   English

Laravel | foreach中的两个数组

[英]Laravel | Two arrays in foreach

I have two different arrays and I want to create a table with these two arrays. 我有两个不同的数组,我想用这两个数组创建一个表。

But I don't know how to realize this in just one foreach loop with Laravel Blade. 但是我不知道如何通过Laravel Blade在一个foreach循环中实现这一点。

The first array is from a external api and the second array was a Laravel Collection I converted to an array with - toArray() 第一个数组来自外部api,第二个数组是Laravel集合,我使用-toArray()将其转换为数组

As you can see both arrays have 10 elements and in my table I want to have each row with columns from both arrays. 如您所见,两个数组都有10个元素,在我的表中,我希望每一行都包含来自两个数组的列。

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 [▶]
]

Can you help me? 你能帮助我吗?

Greets Wipsly 打招呼

The simplest way is to merge the arrays in one array and loop as one array, for example: 最简单的方法是将数组合并为一个数组,然后作为一个数组循环,例如:

@foreach(array_merge($externalApi, $myArray) as $item)
    // ...
@endforeach

Hope there are no duplicate fields in arrays. 希望数组中没有重复的字段。

You can use SPL's MultipleIterator for this: 您可以为此使用SPL的MultipleIterator

$mi = new MultipleIterator();
$mi->attachIterator(new ArrayIterator($array1));
$mi->attachIterator(new ArrayIterator($array2));
foreach($mi as list($array1data, $array2data)) {
    var_dump($array1data,$array2data);
}

Hows about this? 怎么样 array_merge will get it done, but this will show you another option that should be fairly easy to understand. array_merge将完成它,但这将向您显示另一个应该很容易理解的选项。

<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>

EDIT: Though, personally, I really like this MultipleIterator option. 编辑:虽然,就我个人而言,我真的很喜欢这个MultipleIterator选项。

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

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