How to take a value inside an array with PP

Asked

Viewed 73 times

0

I have an API with some information and I want to get the Genre: Samba/Pagode

stdClass Object
(
    [id] => 46484422
    [title] => Raça Negra e Amigos II (Ao Vivo)
    [upc] => 7891430177379
    [link] => https://www.deezer.com/album/46484422
    [share] => https://www.deezer.com/album/46484422?utm_source=deezer&utm_content=album-46484422&utm_term=0_1529271972&utm_medium=web
    [cover] => https://api.deezer.com/album/46484422/image
    [cover_small] => https://e-cdns-images.dzcdn.net/images/cover/45f27e183464715a67145b985dcbbd82/56x56-000000-80-0-0.jpg
    [cover_medium] => https://e-cdns-images.dzcdn.net/images/cover/45f27e183464715a67145b985dcbbd82/250x250-000000-80-0-0.jpg
    [cover_big] => https://e-cdns-images.dzcdn.net/images/cover/45f27e183464715a67145b985dcbbd82/500x500-000000-80-0-0.jpg
    [cover_xl] => https://e-cdns-images.dzcdn.net/images/cover/45f27e183464715a67145b985dcbbd82/1000x1000-000000-80-0-0.jpg
    [genre_id] => 79
    [genres] => stdClass Object
        (
            [data] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 79
                            [name] => Samba/Pagode
                            [picture] => https://api.deezer.com/genre/79/image
                            [type] => genre
                        )

                )

        )

    [label] => Som Livre

My Code:

<?php
  $api = "https://api.deezer.com/album/46484422";
  $url = file_get_contents("$api");
  $json = json_decode($url, true); //This will convert it to an array
  $titulo = $json['title'];
  $capa1000x1000 = $json['cover_xl'];
  $musicas = $json['tracks'];
  $genero = $json['genres'];
?>

<?php echo $genero ?>

However, I wanted to write the name of the genre, but this error:

Notice: Array to string Conversion in api.php on line 29 Array

  • 1

    $var->genres->data[0]->name I guess that’s it.

  • I updated the question.

1 answer

2


Since it’s an array, and there may be more than one genre, you don’t want to have a cycle to catch all genres?

If you just want the first one, it would just be $json['genres']['data'][0]['name'], but if one of these keys, genres, data is not defined, will give error, even if data empty because you’re always trying to access the first.

EDIT

Example of the HTML cycle. In the place on the page where you want to display the generos, you can put something like this. It doesn’t need to be a <ol>, can be any other html element that you then need to use CSS to style.

// ... html anterior que está na pagina ...

<ol>
<?php foreach ($json['genres']['data'] as $genero): ?>
    <li><?php echo $genero ?></li>
<?php endforeach; ?>
</ol>

// ... resto do html
  • And how to do if you don’t have a gender ?

  • You can use the isset. For example, $genero = isset($json['genres']['data'][0]['name']) ? $json['genres']['data'][0]['name'] : "";

  • And if you have more than one gender, how can I list them all ?

  • A cycle. Example, foreach ($json['genres']['data'] as $genero) { echo $genero['name']; }

  • I tested this cycle, have as I put a 01. 02. 03. ates, and one below the other, ie one in each line ?

  • I added an edit to the answer. But if you’re making html, just put the html you need in the cycle to format the genre as needed.

Show 1 more comment

Browser other questions tagged

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