Laravel |两个数组在foreach

问题描述:

我有两个不同的数组,我想用这两个数组创建一个表。Laravel |两个数组在foreach

但我不知道如何在Laravel Blade的一个foreach循环中实现这一点。

第一阵列是从外部API和第二阵列是一个Laravel收集我转换为一个数组与 - 指定者()

正如你可以看到两个阵列具有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 [▶] 
] 

你能帮帮我吗?

电贺 Wipsly

怎么样了这件事吗? 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> 

编辑:虽然,我个人很喜欢这个MultipleIterator选项。

最简单的方法是在一个阵列中并循环到合并数组为一个阵列,例如:

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

希望有在阵列没有重复的字段。

您可以使用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); 
}