在php中将json对象添加到test.json文件中

时间:2016-08-10 14:04:39

标签: php json object multidimensional-array

我有一个json文件test.json,其中包含以下结构

$data[] = '{
                "partgroup": "2",
                "screens": [
                    {
                        "questions": [
                            {
                                "questionno": 1
                            }
                        ]
                    }
                ]
            }';
$json = file_get_contents("test.json", FILE_USE_INCLUDE_PATH);
 foreach($json['test'] as $key=>$value):
     $tempArray = $value['parts'];
 endforeach;
array_push($tempArray, $data);
$jsonData = json_encode($tempArray);
file_put_contents('test.json', $jsonData);

我需要在该部件数组中添加一个partgroup,我的解决方法看起来像这个

.button {
  width: 120px;
  height: 27px;
  outline: none;
  border-radius: 3px;
  box-sizing: border-box;
  cursor: pointer;
}

.solid {
  color: #fff;
  border: none;
  background: linear-gradient(150deg, #3acfd5 0%,#3a89d5 50%,#3a51d5 100%);
}

.thin {
  border-left: none;
  border-right: none;
  border-top: 1px solid #1AD6FD;
  border-bottom: 1px solid #1D62F0;
  
  background-position: 0 0, 100% 0;
  background-repeat: no-repeat;
  background-size: 1px 100%;
  
  background-color: transparent; /* removes button background */
  background-image: 
    linear-gradient(to bottom, #1AD6FD 0%, #1D62F0 100%), 
    linear-gradient(to bottom, #1AD6FD 0%, #1D62F0 100%);
}

这里输出只是在test.json文件末尾附加json。 任何人都可以帮我把数据放在JSON文件的parts部分吗?

0 个答案:

没有答案