I am a beginner to JSON and i'm trying to get values from a json string. For example i want to read the first "t":1443502800 and assing it to a variable $beginTime and the second "t":1443790800 assigned to a variable $endTime
{
"op":"&",
"c":[
{
"type":"date",
"d":">=",
"t":1443502800
},
{
"type":"date",
"d":"<",
"t":1443790800
}
],
"showc":[true,true]
}
I would really appreciate any help :)
Edit code
foreach($rs2 as $record2)
{
if( $record2->availability == '{"op":"&","c":[],"showc":[]}'){
//skip, because the restriction date was not set for this attendance
}
else{
//get the restriction date by making use of json
$json_data = json_decode($record2->availability, true);
echo $json_data['c'];
}
}
The problem is you can't echo an array. You're trying to echo out $json_data['c']
, which is an array. Either return it or echo out what you need. For instance to echo out the start time use echo $json_data['c'][0]['t']
ORIGINAL ANSWER
You should use json_decode without true in the second variable. Then you have objects (started with braces {
) and arrays (started with square brackets [
). You can then get the start and end times us
<?php
$string = '{
"op":"&",
"c":[
{
"type":"date",
"d":">=",
"t":1443502800
},
{
"type":"date",
"d":"<",
"t":1443790800
}
],
"showc":[true,true]
}';
$json = json_decode($string);
echo "Start time: ".$json->c[0]->t;
echo "End time: ".$json->c[1]->t;
Here's an eval.in of it working - https://eval.in/441599
You were almost there. The full call should be to
$beginTime = $json_data['c'][0]['t'];
since $json_data['c'][0]
is the array:
array (
"type" => "date",
"d" => ">=",
"t" => 1443502800
),
The $endtime
can be obtained in a similar manner:
$endTime = $json_data['c'][1]['t'];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.