M
M
malayamarisha2021-02-19 10:17:23
PHP
malayamarisha, 2021-02-19 10:17:23

How to parse json like this?

Good day!
An object like this comes in:

stdClass Object
(
    [return] => [
{
"date":"19.02.2021",
"time_start":"16:00:00",
"duration":900,
"time_end":"16:15:00",
"employee_id":"01234567890"
},
{
"date":"19.02.2021",
"time_start":"16:15:00",
"duration":900,
"time_end":"16:30:00",
"employee_id":"01234567890"
},
{
"date":"20.02.2021",
"time_start":"10:45:00",
"duration":900,
"time_end":"11:00:00",
"employee_id":"gbkdbfndsbvvfnsd"
}
]
)

Tell me how to bring the received data to the form using php:
[0]=>19.02.2021
[1]=>19.02.2021
[2]=>20.02.2021

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Romanenko, 2021-02-19
@Awilum

1. json_decode
https://www.php.net/manual/ru/function.json-decode.php
2. foreach
https://www.php.net/manual/ru/control-structures.f...

T
Tim, 2021-02-19
@Tim-A-2020

<?php
$obj = new stdClass();
$obj->return = '[
    {
    "date":"19.02.2021",
    "time_start":"16:00:00",
    "duration":900,
    "time_end":"16:15:00",
    "employee_id":"01234567890"
    },
    {
    "date":"19.02.2021",
    "time_start":"16:15:00",
    "duration":900,
    "time_end":"16:30:00",
    "employee_id":"01234567890"
    },
    {
    "date":"20.02.2021",
    "time_start":"10:45:00",
    "duration":900,
    "time_end":"11:00:00",
    "employee_id":"gbkdbfndsbvvfnsd"
    }
    ]';
$json = json_decode($obj->return);
$res = array_map(function($item) {
    return $item->date;
}, $json);
var_dump($res);

sandbox.onlinephpfunctions.com/code/6f85a23081a26b...

M
Mikhail Alferov, 2021-02-21
@malferov

<?php

$obj = new stdClass;
$obj->return = '[
    {
    "date":"19.02.2021",
    "time_start":"16:00:00",
    "duration":900,
    "time_end":"16:15:00",
    "employee_id":"01234567890"
    },
    {
    "date":"19.02.2021",
    "time_start":"16:15:00",
    "duration":900,
    "time_end":"16:30:00",
    "employee_id":"01234567890"
    },
    {
    "date":"20.02.2021",
    "time_start":"10:45:00",
    "duration":900,
    "time_end":"11:00:00",
    "employee_id":"gbkdbfndsbvvfnsd"
    }
]';

// JSON-строку (ключ 'return' в объекте) в ассоциативный массив,
// затем из полученного массива выбрать ключи-колонки 'date':
$result = array_column(json_decode($obj->return, true), 'date');

print_r($result);

sandbox.onlinephpfunctions.com/code/11023db15060a2...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question