L
L
lucky_e32016-01-24 09:42:23
PHP
lucky_e3, 2016-01-24 09:42:23

How to correctly access JSON in PHP?

The following response comes from the API:

{
    "status": "ok",
    "meta": {
        "count": 1
    },
    "data": {
        "37677272": [
            {
                "all": {
                    "spotted": 75,
                    "avg_damage_blocked": 65.24,
                    "capture_points": 100,
                    "explosion_hits": 0,
                    "piercings": 299,
                    "xp": 32706,
                    "survived_battles": 11,
                    "dropped_capture_points": 50,
                    "damage_dealt": 37598,
                    "hits_percents": 63,
                    "draws": 0,
                    "battles": 41,
                    "damage_received": 27929,
                    "frags": 56,
                    "direct_hits_received": 189,
                    "hits": 404,
                    "battle_avg_xp": 798,
                    "wins": 20,
                    "losses": 21,
                    "piercings_received": 157,
                    "no_damage_direct_hits_received": 32,
                    "shots": 641,
                    "explosion_hits_received": 4,
                    "tanking_factor": 0.12
                }
            }
        ]
    }
}

It is required to get spotted in the object all.
The name of the array 37677272 is dynamic, it is stored in a variable.
My attempt, described below, failed (the 0 element is taken from the $acid string, and therefore does not find the desired name). Maybe I'm making stupid mistakes, alas, I just started to learn PHP and JSON.
...
$acid = (string)$a_ui;
$obj1->data->$acid[0]->all->spotted;

How to solve the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
synapse_people, 2016-01-24
@lucky_e3

if you need to address the object, then you can write
it like this: $obj1->data->{'37677272'}->all->spotted; - the last option is also working
And this one too:

$s = $obj1->{'data'}->{'37677272'}->{'all'}->{'spotted'};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question