Extract Value In PHP From JSON Array With Diffrent Key

    {
      "code": 200,
      "status": "OK",
      "results": {
        "datetime": [
          {
            "times": {
              "Lunch": "10:00",
              "Dinner": "11:13",
              "Breakfast": "05:01",
              "Tea": "12:25",
              "WorkS": "11:00",
              "WorkE": "10:38",
              
            },
            "date": {
              "timestamp": 1585526400,
              "date": "2020-03-30",
              "working": "Yes"
            }
          }
        ],
        "location": {
          "latitude": 21.416667938232425,
          "longitude": 39.816665649414062,
          "elevation": 333.0,
          "city": "Mumbai",
          "country": "India",
          "country_code": "IN",
          "timezone": "Asia/Dehli",
          "local_offset": 3.0
        },
        "settings": {
          "Defoult": "Computer",
          "Id": "Helloworld",
          "Password": "123r45w2q",
          "PCNo": "2",
          "Cabin": 18A,
          
        }
      }
    }

I Want To Get Time Only For ex in php please help me

                  "Lunch": "10:00",
                  "Dinner": "11:13",
                  "Breakfast": "05:01",
                  "Tea": "12:25",
                  "WorkS": "11:00",
                  "WorkE": "10:38",

Use json_decode to make the JSON string into an object.
Access the datetime section of that object.
Iterate through the times.

1 Like

An Error Showing While Decoding It

My Code :

<?php
$url = "https://api.*****/v2/times/today.json?uiseid=10387546";




// Initialise Url

$jr= curl_init();

//Disible SSL Verification

curl_setopt($jr,CURLOPT_SSL_VERIFYPEER,false);

//Will Return The Response, If False It Printed The Response

curl_setopt($jr,CURLOPT_RETURNTRANSFER,true);

//set url

curl_setopt($jr, CURLOPT_URL , $url);

//execute

$result = curl_exec($jr);

//Closing

curl_close($jr);


//This echo Retiring Perfect Data As Shown Above
    // echo $result;




$F = json_decode($result);

echo $F;













?>

$obj = json_decode($result);
print_r($obj);
1 Like

It’s Working But I Am Unable To Understand How To Extract Value That I want Like Lunch Time Ect…

Try

print_r($obj->datetime->times);

Notice : Undefined property: stdClass::$datetime in C:\xampp\htdocs\add.php on line 43

Notice : Trying to get property ‘times’ of non-object in C:\xampp\htdocs\add.php on line 43

What is the output of the print_r call?

It’s Retaining Data After Decoding

stdClass Object
(
    [code] => 200
    [status] => OK
    [results] => stdClass Object
        (
            [datetime] => Array
                (
                    [0] => stdClass Object
                        (
                            [times] => stdClass Object
                                (
                                    [Lunch] => 04:50
                                    [Dinner] => 06:12
                                    [Breakfast] => 05:00
                                    [Tea] => 12:25
                                    [WorkS] => 15:50
                                    [WorkE] => 18:38
                                    
                                )

                            [date] => stdClass Object
                                (
                                    [timestamp] => 1585612800
                                    [Date] => 2020-03-31
                                    [Working] => Yes
                                )

                        )

                )

            [location] => stdClass Object
                (
                    [latitude] => 21.416667938232
                    [longitude] => 39.816665649414
                    [elevation] => 333
                    [city] => Mecca
                    [country] => Saudi Arabia
                    [country_code] => SA
                    [timezone] => Asia/Riyadh
                    [local_offset] => 3
                )

            [settings] => stdClass Object
                (
                    [Defoult] => Computer
                    [Id] => qer24o9
                    [Password] => 073he642o
                    [Cabin] => 2nd
                   
                )

        )

)

Notice : Undefined property: stdClass::$datetime in C:\xampp\htdocs\add.php on line 43

Notice : Trying to get property ‘times’ of non-object in C:\xampp\htdocs\add.php on line 43

Warning : Use of undefined constant ‘datetime’ - assumed ‘‘datetime’’ (this will throw an Error in a future version of PHP) in C:\xampp\htdocs\add.php on line 43

Fatal error : Uncaught Error: Cannot use object of type stdClass as array in C:\xampp\htdocs\add.php:43 Stack trace: #0 {main} thrown in C:\xampp\htdocs\add.php on line 43

Notice : Trying to get property ‘times’ of non-object in C:\xampp\htdocs\add.php on line 43

The JSON is badly formed. But here is a working version: https://repl.it/@astonecipher/TubbyDeafeningApplicationframework

Sponsor our Newsletter | Privacy Policy | Terms of Service