Reputation: 1
I have a json file as follows and I want to upadate showtime showend and showtitle using the show_id as a key (unique value to identify the show values) using a php and html form page with unicode malayalam support
[
{
"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"
}
]
}
Upvotes: 0
Views: 36
Reputation: 1306
This will work - assuming you are getting a json string and want to end up with a json string when you are done.
// 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) ;
Upvotes: 1