Answer the question
In order to leave comments, you need to log in
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"
}
]
)
[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
1. json_decode
https://www.php.net/manual/ru/function.json-decode.php
2. foreach
https://www.php.net/manual/ru/control-structures.f...
<?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);
<?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);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question