如何从PHP中的json获取数据?

I have such json data structure:

$data='[{"day":0,"periods":[{"start":"01:00","end":"02:00","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"},{"start":"02:30","end":"03:00","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":1,"periods":[{"start":"01:00","end":"02:00","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":2,"periods":[{"start":"01:00","end":"01:30","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":3,"periods":[{"start":"02:00","end":"02:30","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":4,"periods":[{"start":"01:00","end":"01:30","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":5,"periods":[{"start":"01:30","end":"02:00","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"},{"start":"03:00","end":"03:30","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":6,"periods":[]}]';

I want to get all days and start and end time which is in periods.

How to loop threw array and get ? It has 7 days day[0]-day[6] and periods is array which can be empty or have values.

day

periods->start

periods->end

Like everyone is saying you need to use json_decode() to decode the json in to a php variable. When you add TRUE to that function, you will get an associative array. However, no one is telling you how to loop through this array.

One way you could do this is by using foreach(). Because you have a multidimensional array you'll need to use foreach() multiple times.

<?php

$data='[{"day":0,"periods":[{"start":"01:00","end":"02:00","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"},{"start":"02:30","end":"03:00","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":1,"periods":[{"start":"01:00","end":"02:00","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":2,"periods":[{"start":"01:00","end":"01:30","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":3,"periods":[{"start":"02:00","end":"02:30","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":4,"periods":[{"start":"01:00","end":"01:30","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":5,"periods":[{"start":"01:30","end":"02:00","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"},{"start":"03:00","end":"03:30","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":6,"periods":[]}]';

// Decodes a JSON string, When TRUE, returned objects will be converted into associative arrays. 
$array = json_decode($data, true);


/* Output array, uncomment if you would like to see
   echo '<pre>' to make it readable */
// echo '<pre>';
// print_r($array);

// foreach lvl in the array we need to use a new foreach() 
foreach($array as $day) {
    echo 'Day: '. $day['day'] .'<br>';

    foreach($day['periods'] as $periodId => $period) {
        echo 'Period '. $periodId .': '. $period['start'] .' - '. $period['end'] .'<br>';
    }
}

?>

You have to parse the data, which is a string, to an object or array using json_encode

$data=json_encode('[{"day":0,"periods":[]},{"day":1,"periods":[{"start":"08:00","end":"10:00","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"},{"start":"11:00","end":"12:30","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":2,"periods":[{"start":"20:00","end":"00:00","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":3,"periods":[]},{"day":4,"periods":[{"start":"10:00","end":"12:00","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"},{"start":"13:00","end":"14:00","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"},{"start":"15:00","end":"16:00","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":5,"periods":[]},{"day":6,"periods":[]}]');

You can then access it as an object:

echo $data[0]->day;

The compete documentation is here.

you need to decode the json by json_decode(); something like this.

$json = '[{"day":0,"periods":[]},{"day":1,"periods":[{"start":"08:00","end":"10:00","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"},{"start":"11:00","end":"12:30","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":2,"periods":[{"start":"20:00","end":"00:00","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":3,"periods":[]},{"day":4,"periods":[{"start":"10:00","end":"12:00","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"},{"start":"13:00","end":"14:00","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"},{"start":"15:00","end":"16:00","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":5,"periods":[]},{"day":6,"periods":[]}]';
$result = json_decode ($json);

And get data like this.

echo $result[1]->periods[0]->end;