【问题标题】:how to run two foreach loop in one(laravel)如何同时运行两个 foreach 循环(laravel)
【发布时间】:2019-07-16 16:34:18
【问题描述】:

您好,我的代码包含两个数组,我想在表中使用 foreach() 循环回显它们,但存在 php 不允许我在一个循环中运行两个数组的问题

像这样:

@foreach ($playerinfo1 as $info1 && $playerinfo2 as $info2)
            <tr>
                        <td>{{$info1->id}}</td>
                        <td>{{$info1->authid}}</td>
                        <td>{{$info1->nick}}</td>
                        <td>{{$info1->date}}</td>
                        <td>{{$info1->uses}}</td>
                        <td>{{$info2->rounds}}</td>
                        <td>{{$info2->time}}</td>
                        <td>{{$info2->connects}}</td>
                        <td><a href="http://ip-api.com/#{{$info1->ip}}"> 
{{$info1->ip}}</a></td>
                   </tr>
        @endforeach

我尝试了这些代码,但它们有问题并且它们多次回显该值:

        @foreach ($playerinfo1 as $info1)
        <tr>
                    <td>{{$info1->id}}</td>
                    <td>{{$info1->authid}}</td>
                    <td>{{$info1->nick}}</td>enter code here
                    <td>{{$info1->date}}</td>
                    <td>{{$info1->uses}}</td>
                    <td><a href="http://ip-api.com/#{{$info1->ip}}">

{{$info1->ip}}</a></td>
                @foreach ($playerinfo2 as $info2)
                        <td>{{$info2->rounds}}</td>
                        <td>{{$info2->time}}</td>
                        <td>{{$info2->connects}}</td>
             </tr>
            @endforeach
    @endforeach

    @foreach ($playerinfo1 as $info1)
        <tr>
                    <td>{{$info1->id}}</td>
                    <td>{{$info1->authid}}</td>
                    <td>{{$info1->nick}}</td>
                    <td>{{$info1->date}}</td>
                    <td>{{$info1->uses}}</td>
                    <td><a href="http://ip-api.com/#{{$info1->ip}}">{{$info1->ip}}</a></td>
            @foreach ($playerinfo2 as $info2)
                    <td>{{$info2->rounds}}</td>
                    <td>{{$info2->time}}</td>
                    <td>{{$info2->connects}}</td>
         </tr>
        @endforeach
    @endforeach

    @foreach ($playerinfo1 as $info1)
        <tr>
                    <td>{{$info1->id}}</td>
                    <td>{{$info1->authid}}</td>
                    <td>{{$info1->nick}}</td>
                    <td>{{$info1->date}}</td>
                    <td>{{$info1->uses}}</td>
                    <td><a href="http://ip-api.com/#{{$info1->ip}}">{{$info1->ip}}</a></td>
            @endforeach
            @foreach ($playerinfo2 as $info2)
                    <td>{{$info2->rounds}}</td>
                    <td>{{$info2->time}}</td>
                    <td>{{$info2->connects}}</td>
         </tr>
        @endforeach

我还使用 array_combine() 来合并两个数组,但由于键和值之间的差异,它不起作用

$playerinfo1(数组):

array:15 [▼
  0 => {#213 ▼
    +"id": 1
    +"authid": "STEAM_0:0:546411185"
    +"nick": "BesTKiLLeR"
    +"date": "2019-05-24 21:22:25"
    +"uses": 62
    +"ip": "188.211.128.180"
  }
  1 => {#215 ▼
    +"id": 2
    +"authid": "STEAM_0:0:21578434"
    +"nick": "ArTTam"
    +"date": "2019-05-23 22:29:43"
    +"uses": 21
    +"ip": "86.55.174.70"
  }
  2 => {#216 ▶}
  3 => {#217 ▶}
  9 => {#223 ▶}
  10 => {#224 ▶}
  11 => {#225 ▶}
  12 => {#226 ▶}
  26 => {#240 ▶}
  32 => {#246 ▶}
  34 => {#248 ▶}
  35 => {#249 ▶}
  38 => {#252 ▶}
  39 => {#253 ▶}
  45 => {#259 ▶}
]

$playerinfo2(数组):

array:16 [▼
  0 => {#264 ▼
    +"id": 1
    +"connects": 65
    +"rounds": 305
    +"time": 38579
  }
  1 => {#265 ▼
    +"id": 2
    +"connects": 37
    +"rounds": 124
    +"time": 17257
  }
  2 => {#266 ▶}
  3 => {#267 ▶}
  4 => {#268 ▶}
  5 => {#269 ▶}
  6 => {#270 ▶}
  7 => {#271 ▶}
  8 => {#272 ▶}
  9 => {#273 ▶}
  10 => {#274 ▶}
  11 => {#275 ▶}
  12 => {#276 ▶}
  13 => {#277 ▶}
  14 => {#278 ▶}
  15 => {#279 ▶}
]

如果我不能表达我的意思,但我希望你能通过查看此代码了解我的意思enter code here

【问题讨论】:

  • 这两个数组是如何连接的?它们的长度不同($playerinfo2$playerinfo1 多一个元素)。你会怎么处理?数据从何而来?如果它是一个数据库,并且在一个表中存在对另一个表的引用,那么您应该使用 Joins 直接将所有数据作为一个数组获取。
  • 只是为了回答您在标题中提出的问题,您不能使用一个foreach 遍历两个数组。一个foreach 只能遍历一个数组。虽然还有其他解决方法,但我们需要更多信息(如第一条评论中询问的信息)。
  • stackoverflow.com/questions/4480803/two-arrays-in-foreach-loop 向您展示了如何在一个foreach() 中使用两个数组,但它假定数组具有相同的键值。您可能希望使用array_column() 创建新数组,这些数组由"id" 索引(例如)
  • 尝试使用关系laravel docs

标签: php laravel


【解决方案1】:

您正在使用 Laravel,并且您的数据来自两个表。为什么不使用雄辩的关系?这会容易得多。

@foreach ($players as $player)
    <tr>

        <td>{{$player->id}}</td>
        <td>{{$player->authid}}</td>
        <td>{{$player->nick}}</td>enter code here
        <td>{{$player->date}}</td>
        <td>{{$player->uses}}</td>
        <td><a href="http://ip-api.com/#{{$player->ip}}">{{$player->ip}}</a></td>

        <!-- Use a relationship (e.g., stats) to get additional data -->
        <td>{{$player->stats->rounds}}</td>
        <td>{{$player->stats->time}}</td>
        <td>{{$player->stats->connects}}</td>

    </tr>
@endforeach

【讨论】:

  • 谢谢你的回答我不知道该怎么做你能建立关系吗?我的控制器(只需输入名称):collabedit.com/cypyd
  • 这是仅有的两张与玩家相关的表格,还是还有一个主要的players 表格?只是想弄清楚你的数据库是如何组织的。
  • 而且每个表中的id都一样(比如1对1、2对2等)?另一方面,我担心您的-&gt;unique() 电话可能具有破坏性。具有相同 IP 的记录将从结果中删除,即使它们是不同的玩家。
  • 另外,你有在 Laravel 中定义的模型吗?
  • 是的,因为我的插件有时会创建重复的结果,所以我必须通过 ip 使用 unique() 删除重复
【解决方案2】:

您可以在控制器中使用 array_merge($arr1,$arr2) 合并两个数组

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-09
    • 2011-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多