douxie1894 2019-07-16 16:34
浏览 210
已采纳

如何在一个中运行两个foreach循环(laravel)

Hi my code is including two array which I want echo them with foreach() loop in table but there is problem that php won’t let me run two array in one loop

like this:

@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

I tried these codes but they have problem and they echo the value more than once:

        @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

Also I used array_combine() in order to combining two array but because of differences between key and value ,it doesn't work

$playerinfo1(array):

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

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

I’m afraid if I could not make my point but I hope you will understand what am I mean by looking this codeenter code here

  • 写回答

2条回答 默认 最新

  • dsa2c2255888 2019-07-16 18:13
    关注

    You are using Laravel and your data is coming from two tables. Why not use an eloquent relationship? It would be so much easier.

    @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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的