2015-11-05 3 views
0

У меня есть два разных массива, и я хочу создать таблицу с этими двумя массивами.Laravel | Два массива в foreach

Но я не знаю, как реализовать это только в одном цикле foreach с Laravel Blade.

Первый массив из внешнего API, а второй массив был Laravel Collection I преобразуется в массив с - ToArray()

Как вы можете видеть оба массива имеют 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 [▶] 
] 

Помогите мне?

Greets Wipsly

ответ

0

Хау по этому поводу? 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: Хотя, лично, мне очень нравится этот вариант MultipleIterator.

1

Самый простой способ заключается в объединении массивов в один массив и петля в одном массиве, например:

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

Надежда нет повторяющихся полей в массивы.

1

Вы можете использовать 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); 
} 
Смежные вопросы