我正在尝试使用php脚本将内容附加到json文件。
file1的输出是
[
{"id":"drives","name":"hddcntrlr"},
{"id":"drives","name":"diskdrivedes"}
]
file2的输出是
[
{"id":"filters","name":"filter_supptype_cp_01"},
{"id":"drives","name":"hddcntrlr"},
{"id":"drives","name":"diskdrivedes"}
]
附加输出后应为:
[
{"id":"filters","name":"filter_supptype_cp_01"}]
[{"id":"drives","name":"hddcntrlr"},
{"id":"drives","name":"diskdrivedes"}
]
但输出结果如下: -
$file = file_get_contents('makejson.json');
$data = json_decode($file);
$info[] = array('id'=>$attribute1, 'name'=>$sub_attr_name);
$newb = array_values((array)$info);
file_put_contents('makejson.json',json_encode($newb),FILE_APPEND);
我尝试的代码是:
<?php
phpinfo();
请帮助!!!!
答案 0 :(得分:0)
解码两个文件,追加数组,再次编码:
$existingData = json_decode(file_get_contents('file1.json'));
$newData = json_decode(file_get_contents('file2.json'));
$existingData = array_merge($existingData, $newData);
file_put_contents('file1.json', json_encode($existingData));
答案 1 :(得分:0)
array_push()函数将一个或多个元素插入到数组的末尾。
您的代码:
$file = file_get_contents('makejson.json');
$tempArray = json_decode($file);
array_push($tempArray, $data); // $data is your new data
$jsonData = json_encode($tempArray);
file_put_contents('makejson.json', $jsonData);
答案 2 :(得分:-1)
您不需要BB1_MODULES = {
'BB103': Module('BB103', 'Computer Systems', 5),
'BB111': Module('BB111', 'Web Design', 5),
'BB112': Module('BB112', 'Digital Innovation', 5),
'BB123': Module('BB123', 'Computer Programming I', 10),
'BB124': Module('BB124', 'Computer Programming II', 10),
'BB343': Module('BB343', 'Networks and Internet', 5),
'BB434': Module('BB434', 'Operating Systems', 5),
'BB543': Module('BB543', 'Problem Solving', 5),
'BB432': Module('BB432', 'IT Mathematics', 10)}#ects
class Student(object):
def __init__(self, idnum, surname, firstname, mods=CA1_MODULES):
self.__idnum = idnum
self.__surname = surname
self.__firstname = firstname
self.__mods = mods
self.__marks = {code: 0 for code in self.__mods.keys()} #returm a list of module codes #dict comprehension
# self.__total_credits
self.__total__credits = sum[mod.ects for mod in self.__mods.values()]
def __str__(self):
name = '{} : {} {}'.format(self.__idnum,
self.__firstname,
self.__surname)
uline = '-' * len(name)
results = '\n'.join([code + ' : ' + self.__mods[code].title +
' : ' + str(self.__marks[code])
for code in sorted(self.__mods.keys())])
pm = 'Precision mark: {:.2f}'.format(self.precision_mark())
if self.passed():
outcome = 'Pass'
elif self.passed_by_compensation():
outcome = 'Pass by compensation'
else:
outcome = 'Fail'
return '\n'.join([name, uline, results, pm, outcome])
def add_mark(self, module, mark):
self.__marks[module]=mark
def precision_mark():
[self.__marks[mod] * self.__mods[code].ects for code in self.__mods.keys()] / self.__total_credits
def passed():
return min(self__marks.values()) >= 40
def passed_by_compensation():
if self.precision_mark() <45:
return False
sum([self.__mods[code].ects for code in self.__mods.keys() if self.marks[code] < 40]) > self.__total_credits / 6:
return False
return min(self.marks.__values()) >= 35
self.__total__credits = sum[mod.ects for mod in self.__mods.values()]
^
#SyntaxError: invalid syntax
#self.__total__credits = sum[mod.ects for mod in self.__mods.values()]
阵列。您需要append
。
尝试:
overwrite
它将用新数组覆盖。