我有一个json文件,如下所示,我想要更新showtime showend和showtitle,使用show_id作为关键字(唯一值来标识显示值),使用具有unicode马拉雅拉姆支持的php和html表单页面。
[
{
"day": "Sunday",
"events": [
{
"show_id": "6231",
"show_time": "02:00",
"show_time_end": "03:00",
"show_title": "SundayProgram5"
},
{
"show_id": "6232",
"show_time": "03:00",
"show_time_end": "04:00",
"show_title": "SundayProgram6"
},
{
"show_id": "6234",
"show_time": "04:00",
"show_time_end": "05:00",
"show_title": "SundayProgram7"
},
{
"show_id": "6235",
"show_time": "05:00",
"show_time_end": "06:00",
"show_title": "SundayProgram8"
}
]
}
发布于 2018-06-22 09:38:34
这将会起作用-假设您正在获取一个json字符串,并且想要在完成后得到一个json字符串。
// your data as a json string
$str = '{
"day": "Sunday",
"events": [
{
"show_id": "6231",
"show_time": "02:00",
"show_time_end": "03:00",
"show_title": "SundayProgram5"
},
{
"show_id": "6232",
"show_time": "03:00",
"show_time_end": "04:00",
"show_title": "SundayProgram6"
},
{
"show_id": "6234",
"show_time": "04:00",
"show_time_end": "05:00",
"show_title": "SundayProgram7"
},
{
"show_id": "6235",
"show_time": "05:00",
"show_time_end": "06:00",
"show_title": "SundayProgram8"
}
]
}' ;
// create an array out of your data
$json = json_decode($str,true) ;
// get your events into an array with the show_id as the index
$jsonByID = array() ;
foreach($json['events'] as $k=>$event) {
$jsonByID[$event['show_id']] = $event ;
}
// update your values using show_id
// your code here
// example just for testing demonstrations
$jsonByID[6235]['show_title'] = 'test' ;
// get your array back into the original format
foreach($json['events'] as $k=>$event) {
$json['events'][$k] = $jsonByID[$event['show_id']] ;
}
// back to a json string
$updatedJson = json_encode($json) ;
https://stackoverflow.com/questions/50978576
复制相似问题