0

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

  • How are those two arrays connected? They are of different lengths (`$playerinfo2` has one more element than `$playerinfo1`). How would you handle that? Where does the data come from? If it's a database and there are a reference in one table to the other, then you should just fetch all the data directly as one single array, using Joins. – M. Eriksson Jul 16 '19 at 16:39
  • Just to answer the question you ask in the title, you can't iterate through two arrays using one `foreach`. One `foreach` can only iterate through one array. There are other ways of solving it though, but then we need more information (like the info asked in the first comment). – M. Eriksson Jul 16 '19 at 16:44
  • https://stackoverflow.com/questions/4480803/two-arrays-in-foreach-loop shows you how to use two arrays in one `foreach()`, but it assumes that the arrays have the same key value. You may want to use `array_column()` to create new arrays which are indexed (for example) by `"id"` – Nigel Ren Jul 16 '19 at 17:01
  • Possible duplicate of [Two arrays in foreach loop](https://stackoverflow.com/questions/4480803/two-arrays-in-foreach-loop) – Sabee Jul 16 '19 at 17:37
  • Try to use relationship [laravel docs](https://laravel.com/docs/5.8/eloquent-relationships#one-to-one) – Giang D.MAI Jul 17 '19 at 02:33

2 Answers2

0

You can use array_merge($arr1,$arr2) in your controller to combine two arrays

mohamed hassan
  • 479
  • 3
  • 6
0

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
matticustard
  • 4,850
  • 1
  • 13
  • 18
  • thank you for your answer i don't know how to exactly do this can you make a relation? my controller(just enter the name):http://collabedit.com/cypyd –  Jul 16 '19 at 19:37
  • Are these the only two tables related to players, or is there also a main `players` table? Just trying to figure out how your database is organized. – matticustard Jul 16 '19 at 21:02
  • And the `id` in each table are the same (e.g., 1 to 1, 2 to 2, etc.)? On a side note, I'm concerned that your `->unique()` call is potentially destructive. Records with the same IP will be removed from the results, even if they are different players. – matticustard Jul 16 '19 at 21:53
  • Also, do you even have any models defined in Laravel? – matticustard Jul 16 '19 at 22:16
  • yes because my plugin sometimes create duplicate of result so i have to delete the duplicates with unique() by ip –  Jul 16 '19 at 22:18
  • no i do not have model but if is necessary i can make it –  Jul 16 '19 at 22:20
  • What plugin? I'm concerned I just don't know enough about your setup and you don't seem to already be using Laravel's MVC principles. You may want to take a look at Laracasts to get a good understanding of how to use Laravel as intended. https://laracasts.com/series/laravel-from-scratch-2018 – matticustard Jul 16 '19 at 22:24
  • my whole project is relative to information of game players which the information of them world be confirm on mysql database by plugin automatically but sometimes the plugin confirm the information of players for more than once so i have to delete them with unique() i would appreciate if you make relation for me –  Jul 17 '19 at 17:55
  • I would really like to be able to help you, but at this point I'm not even sure how your tables are related. Do the `id`s even match in the corresponding tables, or are you just trying to line them up manually? You are using `->unique()` to delete rows from one set of data and not the other, which will shift data into different rows. How am I supposed to believe the rows in each set of data actually match up with each other? There seem to be problems with your methods of storing and retrieving data that need to be addressed before I can do anything to help. – matticustard Jul 17 '19 at 18:54