Foreach inside foreach with PHP

Asked

Viewed 296 times

0

I searched here on the site and did not find something that could help me in my case, I have a foreach where I take the data from the Deezer API and have another that takes the data from the YOUTUBE API,

I want to take the title and music information and have it search the youtube api and show in the table the link to the video, but I don’t know how to 'integrate' the two, the way it is, it returns me only in the last line.

<table class="table table-sm">
            <thead>
                <tr>
                    <th width="100px">Album</th>
                    <th width="175px">Artista</th>
                    <th style = 'text-align: left'width="500px">Música</th>
                    <th scope="col">Preview</th>
                    <th scope="col">Ações</th>

                </tr>
            </thead>
            <tbody>
                <!-- DEEZER API -->
                <?php


                $url = 'https://api.deezer.com/2.0/search?q=Adam%20Turley';

                $jsonStr = file_get_contents($url);
                $jsonArr = json_decode($jsonStr, true);

                $nome = array();
                $titulo = array();
                $musica = array();
                $album = array();
                $link = array();
                $j = 0;
                $i = 0;
                foreach (array_slice($jsonArr['data'],0,10) as $row) {
                $nome[] = $row['artist']['name']; //artista
                $titulo[] = $row['title'];//nome da música
                $musica[] = $row['preview'];//audio de 30 segundos
                $album[] = $row['album']['cover_xl'];//foto do album 
                $titulo2 = str_replace(' ', '%20', $titulo[$i]);
                $titulo2;
                $link = $row['link']; //link deezer
                $i++;




                ?>



                <tr >
                    <td >
                        <img src="<?php echo $row['album']['cover_xl'] ?>" width='50px' height='50px' class="rounded-circle">    

                    </td>
                    <td style="padding-top: 15px"><?php echo $row['artist']['name'] ?></td>
                    <td style="padding-top: 15px"><?php echo $row['title'] ?></td>
                    <td style="padding-top: 12px">

                        <audio id="audio<?php echo $j?>" src="<?php echo $row['preview'] ?>"></audio>


                        <button data-id="audio<?php echo $j?>" class="btn" style='color: #D8D8D8'><i class="fas fa-play"></i></button>  


                    </td>
                    <td style="padding-top: 15px">
                        <a href="<?php echo $link ?>"target="_blank" style="color:#FA3974"><i class="fas fa-music fa-1x"></i></a>
                    <!-- YOUTUBE API -->    
                    <?php
                    }

                    $url2 = 'https://www.googleapis.com/youtube/v3/search?q=Adam%20Turley%20Heartless&key=minhachave&maxResults=1&part=snippet';

                    $jsonStr2 = file_get_contents($url2);
                    $jsonArr2 = json_decode($jsonStr2, true);

                    $videoId2 = array();


                    foreach ($jsonArr2['items'] as $row2) {
                    $videoId2[] = $row2['id']['videoId']; //id do vídeo




      ?>

      <a href="https://www.youtube.com/watch?v=<?php echo $videoId2[] = $row2['id']['videoId']; ?>" style = 'color:#CA2E31' target="_blank"><i class="fab fa-youtube fa-1x"></i>

      <?php  }   ?>

  </td>
</tr>


</tbody>
</table>

1 answer

1


You are closing the first foreach keys before opening the new foreach. This is happening above the line containing the $url2. Remove this key and place together the second foreach lock key.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.