0

I'm using PHP,I have decoded JSON, for access data from json array I'm using this notation:

$slopes_total = $myArray['items'][0]['slopes-total'];

below is one part of decoded JSON

"items":[
        {
            "valley-run":false,
            "slopes-opened":60,
            "slopes-total":120,
            "forecast":{
                "it":{
                    "1576748703":"10:Nubi fitte, ma solo deboli piogge.",
                    "1576835103":"10:Nubi fitte, ma solo deboli piogge.",
                    "1576921503":"10:Nubi fitte, ma solo deboli piogge."
                },
                "de":{
                    "1576748703":"10:Dicke Wolken, aber nur leichter Regen.",
                    "1576835103":"10:Dicke Wolken, aber nur leichter Regen.",
                    "1576921503":"10:Dicke Wolken, aber nur leichter Regen."
                },
                "en":{
                    "1576748703":"10:Thick cloud cover but little rain only forecast.",
                    "1576835103":"10:Thick cloud cover but little rain only forecast.",
                    "1576921503":"10:Thick cloud cover but little rain only forecast."
                }
            },

I have no problem to access: valley-run, slopes_opened..., the problem starts when I want to access: forecast en (data inside this array) in need to access each of 3 timestamp values and each of meteo descriptions.

7
  • Please show the attempts that didn't work? $myArray['items'][0]['forecast']['it']['1576748703'] should work. Commented Dec 18, 2019 at 14:43
  • Yes, it’s working, but the problem is that '1576748703' is a variable value and i don`t know it future value , so Ii can’t access is in this way Commented Dec 18, 2019 at 14:50
  • What about "it", do you know that value, or do you want to get it? Commented Dec 18, 2019 at 14:51
  • no, JSON data is brought to us by skiresort company, we don`t have possibility to modify it. So i i need to get the timestamp value. Commented Dec 18, 2019 at 14:54
  • I meant something else: what about the language code... do you know the one for which you want to get the timestamps? Commented Dec 18, 2019 at 14:56

1 Answer 1

1

Your data is presented in sort of JSON encoding. So first of all make sure your JSON is decoded with passing true as second argument to json_decode.

If you know the language code -- for instance "en" -- you can do:

foreach ($myArray['items'][0]['forecast']['en'] as $timestamp => $weather) {
    echo "$timestamp - $weather\n";
}

If you just need the timestamps alone, without the corresponding weather description, then:

$timestamps = array_keys($myArray['items'][0]['forecast']['en']);

If you don't know the language code, and don't care, but just want to get the timestamps from any of the language sets:

$timestamps = array_keys(reset($myArray['items'][0]['forecast']));

If you want to know which are the available languages:

$languages = array_keys($myArray['items'][0]['forecast']);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.