【问题标题】:Laravel | Two arrays in foreach拉拉维尔 | foreach 中的两个数组
【发布时间】:2016-02-06 18:04:58
【问题描述】:

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

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

第一个数组来自外部 api,第二个数组是 Laravel 集合,我使用 -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 [▶]
]

你能帮帮我吗?

问候 轻飘飘的

【问题讨论】:

    标签: php laravel foreach


    【解决方案1】:

    这个怎么样? 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 选项。

    【讨论】:

      【解决方案2】:

      最简单的方法是将数组合并为一个数组,循环为一个数组,例如:

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

      希望数组中没有重复的字段。

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 2020-12-29
          • 1970-01-01
          • 2017-09-02
          • 2015-08-24
          • 1970-01-01
          • 2023-03-29
          • 1970-01-01
          • 2017-04-20
          • 2018-03-06
          相关资源
          最近更新 更多