0

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"

          }
  ]
}
1
  • Okay. What have you tried so far? I'd recommend checking out the SO tour page (stackoverflow.com/tour) - namely, in the Don't ask about... section: Questions you haven't tried to find an answer for (show your work!). Commented Jun 21, 2018 at 23:18

1 Answer 1

1

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) ;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.