M
M
Maxim Kiktev2016-01-30 05:49:48
PHP
Maxim Kiktev, 2016-01-30 05:49:48

How to parse an array into variables and write to the database?

I get an array from the data via api

$array = array();

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://shikimori.org/api/animes/31637');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "User Agent");
if ($content = curl_exec($ch)) {
  $array = json_decode($content, true);  
}
curl_close($ch);

echo '<pre>';
print_r($array);

array
Array
(
    [id] => 31637
    [name] => Gate: Jieitai Kanochi nite, Kaku Tatakaeri 2nd Season
    [russian] => Врата: Там бьются наши воины — Огнедышащие драконы
    [image] => Array
        (
            [original] => /images/anime/original/31637.jpg?1453660380
            [preview] => /images/anime/preview/31637.jpg?1453660380
            [x96] => /images/anime/x96/31637.jpg?1453660380
            [x48] => /images/anime/x48/31637.jpg?1453660380
        )

    [url] => /animes/31637-gate-jieitai-kanochi-nite-kaku-tatakaeri-2nd-season
    [kind] => tv
    [ongoing] => 1
    [anons] => 
    [status] => ongoing
    [episodes] => 12
    [episodes_aired] => 4
    [rating] => r
    [english] => Array
        (
        )

    [japanese] => Array
        (
            [0] => GATE(ゲート)自衛隊 彼の地にて、斯く戦えり 第2クール
        )

    [synonyms] => Array
        (
            [0] => Gate: Thus the JSDF Fought There! Fire Dragon Arc
            [1] => Gate: Jieitai Kanochi nite
            [2] => Kaku Tatakaeri - Enryuu-hen
            [3] => Gate Jieitai Kanochi Nite Kaku Tatakaeri 2nd Season
        )

    [aired_on] => 2016-01-09
    [released_on] => 2016-03-26
    [duration] => 23
    [score] => 8.17
    [description] => Second season of the [i]Gate[/i] anime. Adapts Enryuu-hen, and Douran-hen arcs.
    [description_html] => Second season of the Gate anime. Adapts Enryuu-hen, and Douran-hen arcs.
    [favoured] => 
    [thread_id] => 184926
    [world_art_id] => 0
    [myanimelist_id] => 31637
    [ani_db_id] => 0
    [rates_scores_stats] => Array
        (
            [0] => Array
                (
                    [name] => 10
                    [value] => 231
                )

            [1] => Array
                (
                    [name] => 9
                    [value] => 99
                )

            [2] => Array
                (
                    [name] => 8
                    [value] => 84
                )

            [3] => Array
                (
                    [name] => 7
                    [value] => 35
                )

            [4] => Array
                (
                    [name] => 6
                    [value] => 15
                )

            [5] => Array
                (
                    [name] => 5
                    [value] => 3
                )

            [6] => Array
                (
                    [name] => 3
                    [value] => 1
                )

            [7] => Array
                (
                    [name] => 1
                    [value] => 1
                )

        )

    [rates_statuses_stats] => Array
        (
            [0] => Array
                (
                    [name] => Запланировано
                    [value] => 2303
                )

            [1] => Array
                (
                    [name] => Смотрю
                    [value] => 2023
                )

            [2] => Array
                (
                    [name] => Просмотрено
                    [value] => 34
                )

            [3] => Array
                (
                    [name] => Отложено
                    [value] => 52
                )

            [4] => Array
                (
                    [name] => Брошено
                    [value] => 7
                )

        )

    [genres] => Array
        (
            [0] => Array
                (
                    [id] => 2
                    [name] => Adventure
                    [russian] => Приключения
                    [kind] => anime
                )

            [1] => Array
                (
                    [id] => 1
                    [name] => Action
                    [russian] => Экшен
                    [kind] => anime
                )

            [2] => Array
                (
                    [id] => 38
                    [name] => Military
                    [russian] => Военное
                    [kind] => anime
                )

            [3] => Array
                (
                    [id] => 10
                    [name] => Fantasy
                    [russian] => Фэнтези
                    [kind] => anime
                )

        )

    [studios] => Array
        (
            [0] => Array
                (
                    [id] => 56
                    [name] => A-1 Pictures Inc.
                    [filtered_name] => A-1 Pictures Inc.
                    [real] => 1
                    [image] => /images/studio/original/56.?1434707196
                )

        )

    [videos] => Array
        (
            [0] => Array
                (
                    [id] => 11435
                    [url] => http://youtu.be/oPfeTTcZGhs
                    [image_url] => http://shikimori.org/images/31637
                    [player_url] => http://youtube.com/embed/oPfeTTcZGhs
                    [name] => 
                    [kind] => PV
                    [hosting] => youtube
                )

            [1] => Array
                (
                    [id] => 12579
                    [url] => http://youtu.be/TrwXsOTWUeU
                    [image_url] => http://shikimori.org/images/31637
                    [player_url] => http://youtube.com/embed/TrwXsOTWUeU
                    [name] => 
                    [kind] => PV
                    [hosting] => youtube
                )

        )

    [screenshots] => Array
        (
        )

    [user_rate] => 
)

How to parse it into variables for writing to the database?
+ there will even be a link or information where to dig in this direction

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
crazyhat, 2016-02-01
@Ne0lite

Why break it into variables? You can use array elements for writing. For example:
$array['id]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question