0

I'm trying to loop through an array of moves for a Pokemon website. I'm using the API PokeApi (https://pokeapi.co/).

My question is how can I access the moves in these arrays using plain PHP. I've tried using this just to call 1 move. But I don't know how to access data that's in arrays. Like "move->version-group-details".

$base = "https://pokeapi.co/api/v2/pokemon/";
$id = 1;
$data = file_get_contents($base.$id."/");
$pokemon = json_decode($data);

echo $pokemon->moves[0];

sample

Thanks in advance :)

  • 2
    You seem to have used notation for arrays and objects already. You could get a move name like `$pokemon->moves[0]->move->name`, or you could get the first element of `version_group_details` with `$pokemon->moves[0]->version_group_details[0]`, and so on. – Aioros Jan 16 '19 at 19:32
  • 2
    Actually, you are trying to do this with an Object (Not an Array). If you want an array, instead of `json_decode($data);` do `json_decode($data, true);`. As an array, you could access it with `$pokemon['moves'][0]['version_group_details'][0-2]` – GrumpyCrouton Jan 16 '19 at 19:32
  • Ok thanks alot! It worked. But what should I do if I want to loop all moves of that specific Pokemon. – BellenBlaasBoss Jan 16 '19 at 19:51

1 Answers1

2

So you have two methods here you can do so when I run

$base = "https://pokeapi.co/api/v2/pokemon/";
$id = 1;
$data = file_get_contents($base.$id."/");
$pokemon = json_decode($data);

print_r($pokemon->moves[0]);

I get the result:

stdClass Object
(
    [move] => stdClass Object
        (
            [name] => razor-wind
            [url] => https://pokeapi.co/api/v2/move/13/
        )

    [version_group_details] => Array
        (
            [0] => stdClass Object
                (
                    [level_learned_at] => 0
                    [move_learn_method] => stdClass Object
                        (
                            [name] => egg
                            [url] => https://pokeapi.co/api/v2/move-learn-method/2/
                        )

                    [version_group] => stdClass Object
                        (
                            [name] => crystal
                            [url] => https://pokeapi.co/api/v2/version-group/4/
                        )

                )

            [1] => stdClass Object
                (
                    [level_learned_at] => 0
                    [move_learn_method] => stdClass Object
                        (
                            [name] => egg
                            [url] => https://pokeapi.co/api/v2/move-learn-method/2/
                        )

                    [version_group] => stdClass Object
                        (
                            [name] => gold-silver
                            [url] => https://pokeapi.co/api/v2/version-group/3/
                        )

                )

        )

)

If you want to access a moves name you will have to run $pokemon->moves[0]->move->name since we are getting an object returned. If you want to get the name inside the move_learn_method of version_group_details you will have to run

$pokemon->moves[0]->version_group_details[0]-> move_learn_method->name

Alternatively, if you want to return all arrays instead of objects just run this

    $base = "https://pokeapi.co/api/v2/pokemon/";
    $id = 1;
    $data = file_get_contents($base.$id."/");
    $pokemon = json_decode($data, true);

    print_r($pokemon['moves'][0]);

This will now return

Array
(
    [move] => Array
        (
            [name] => razor-wind
            [url] => https://pokeapi.co/api/v2/move/13/
        )

    [version_group_details] => Array
        (
            [0] => Array
                (
                    [level_learned_at] => 0
                    [move_learn_method] => Array
                        (
                            [name] => egg
                            [url] => https://pokeapi.co/api/v2/move-learn-method/2/
                        )

                    [version_group] => Array
                        (
                            [name] => crystal
                            [url] => https://pokeapi.co/api/v2/version-group/4/
                        )

                )

            [1] => Array
                (
                    [level_learned_at] => 0
                    [move_learn_method] => Array
                        (
                            [name] => egg
                            [url] => https://pokeapi.co/api/v2/move-learn-method/2/
                        )

                    [version_group] => Array
                        (
                            [name] => gold-silver
                            [url] => https://pokeapi.co/api/v2/version-group/3/
                        )

                )

        )

)

So instead of having to use the object accessor -> you can access the data by using array notation so instead of

$pokemon->moves[0]->version_group_details[0]-> move_learn_method->name

you can now use:

$pokemon['moves']['version_group_details'][0]['move_learn_method']['name']

Hope that helped.

Solomon Antoine
  • 554
  • 1
  • 7
  • 14