首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用html表单和php更新Json密钥

使用html表单和php更新Json密钥
EN

Stack Overflow用户
提问于 2018-06-22 07:07:17
回答 1查看 29关注 0票数 0

我有一个json文件,如下所示,我想要更新showtime showend和showtitle,使用show_id作为关键字(唯一值来标识显示值),使用具有unicode马拉雅拉姆支持的php和html表单页面。

代码语言:javascript
复制
[
    {
      "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"

          }
  ]
}
EN

回答 1

Stack Overflow用户

发布于 2018-06-22 09:38:34

这将会起作用-假设您正在获取一个json字符串,并且想要在完成后得到一个json字符串。

代码语言:javascript
复制
// 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) ;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50978576

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档