我有两个文件的数据在json
格式:
New_data.json
[{
"AUDCAD": {
"MN": {
"t": [1435708800],
"o": [0.9652],
"h": [0.9653],
"l": [0.94],
"c": [0.9508],
"v": [4040241]
},
"W1": {
"t": [1437264000],
"o": [0.9528],
"h": [0.9634],
"l": [0.948],
"c": [0.9508],
"v": [1052713]
},
"D1": {
"t": [1437696000],
"o": [0.95],
"h": [0.9522],
"l": [0.9486],
"c": [0.9508],
"v": [158491]
},
"H4": {
"t": [1437739200],
"o": [0.9501],
"h": [0.9522],
"l": [0.9496],
"c": [0.9508],
"v": [23012]
},
"H1": {
"t": [1437750000],
"o": [0.9513],
"h": [0.9515],
"l": [0.9505],
"c": [0.9508],
"v": [2015]
},
"M30": {
"t": [1437750000],
"o": [0.9513],
"h": [0.9515],
"l": [0.9505],
"c": [0.9508],
"v": [2015]
},
"M15": {
"t": [1437750900],
"o": [0.9513],
"h": [0.9514],
"l": [0.9505],
"c": [0.9508],
"v": [757]
},
"M5": {
"t": [1437751200],
"o": [0.9507],
"h": [0.9512],
"l": [0.9506],
"c": [0.9508],
"v": [159]
},
"M1": {
"t": [1437751260],
"o": [0.951],
"h": [0.9512],
"l": [0.9507],
"c": [0.9508],
"v": [33]
}
},
"AUDCHF": {
"MN": {
"t": [1435708800],
"o": [0.7226],
"h": [0.7262],
"l": [0.6949],
"c": [0.7011],
"v": [1521567]
},
"W1": {
"t": [1437264000],
"o": [0.7064],
"h": [0.7129],
"l": [0.6973],
"c": [0.7011],
"v": [376787]
},
"D1": {
"t": [1437696000],
"o": [0.7007],
"h": [0.7032],
"l": [0.6973],
"c": [0.7011],
"v": [53293]
},
"H4": {
"t": [1437739200],
"o": [0.7001],
"h": [0.7014],
"l": [0.6986],
"c": [0.7011],
"v": [7849]
},
"H1": {
"t": [1437750000],
"o": [0.7009],
"h": [0.7012],
"l": [0.7004],
"c": [0.7011],
"v": [580]
},
"M30": {
"t": [1437750000],
"o": [0.7009],
"h": [0.7012],
"l": [0.7004],
"c": [0.7011],
"v": [580]
},
"M15": {
"t": [1437750900],
"o": [0.7009],
"h": [0.7012],
"l": [0.7007],
"c": [0.7011],
"v": [154]
},
"M5": {
"t": [1437751200],
"o": [0.7009],
"h": [0.7012],
"l": [0.7009],
"c": [0.7011],
"v": [24]
},
"M1": {
"t": [1437751260],
"o": [0.7011],
"h": [0.7012],
"l": [0.701],
"c": [0.7011],
"v": [9]
}
}
}]
Old_data.json
[{
"AUDCAD": {
"MN": {
"t": [1234,1234,1234,1234,1234,1234,1234],
"o": [1234,1234,1234,1234,1234,1234,1234],
"h": [1234,1234,1234,1234,1234,1234,1234],
"l": [1234,1234,1234,1234,1234,1234,1234],
"c": [1234,1234,1234,1234,1234,1234,1234],
"v": [1234,1234,1234,1234,1234,1234,1234]
},
"W1": {
"t": [1234,1234,1234,1234,1234,1234,1234],
"o": [1234,1234,1234,1234,1234,1234,1234],
"h": [1234,1234,1234,1234,1234,1234,1234],
"l": [1234,1234,1234,1234,1234,1234,1234],
"c": [1234,1234,1234,1234,1234,1234,1234],
"v": [1234,1234,1234,1234,1234,1234,1234]
},
"D1": {
"t": [1234,1234,1234,1234,1234,1234,1234],
"o": [1234,1234,1234,1234,1234,1234,1234],
"h": [1234,1234,1234,1234,1234,1234,1234],
"l": [1234,1234,1234,1234,1234,1234,1234],
"c": [1234,1234,1234,1234,1234,1234,1234],
"v": [1234,1234,1234,1234,1234,1234,1234]
},
"H4": {
"t": [1234,1234,1234,1234,1234,1234,1234],
"o": [1234,1234,1234,1234,1234,1234,1234],
"h": [1234,1234,1234,1234,1234,1234,1234],
"l": [1234,1234,1234,1234,1234,1234,1234],
"c": [1234,1234,1234,1234,1234,1234,1234],
"v": [1234,1234,1234,1234,1234,1234,1234]
},
"H1": {
"t": [1234,1234,1234,1234,1234,1234,1234],
"o": [1234,1234,1234,1234,1234,1234,1234],
"h": [1234,1234,1234,1234,1234,1234,1234],
"l": [1234,1234,1234,1234,1234,1234,1234],
"c": [1234,1234,1234,1234,1234,1234,1234],
"v": [1234,1234,1234,1234,1234,1234,1234]
},
"M30": {
"t": [1234,1234,1234,1234,1234,1234,1234],
"o": [1234,1234,1234,1234,1234,1234,1234],
"h": [1234,1234,1234,1234,1234,1234,1234],
"l": [1234,1234,1234,1234,1234,1234,1234],
"c": [1234,1234,1234,1234,1234,1234,1234],
"v": [1234,1234,1234,1234,1234,1234,1234]
},
"M15": {
"t": [1234,1234,1234,1234,1234,1234,1234],
"o": [1234,1234,1234,1234,1234,1234,1234],
"h": [1234,1234,1234,1234,1234,1234,1234],
"l": [1234,1234,1234,1234,1234,1234,1234],
"c": [1234,1234,1234,1234,1234,1234,1234],
"v": [1234,1234,1234,1234,1234,1234,1234]
},
"M5": {
"t": [1234,1234,1234,1234,1234,1234,1234],
"o": [1234,1234,1234,1234,1234,1234,1234],
"h": [1234,1234,1234,1234,1234,1234,1234],
"l": [1234,1234,1234,1234,1234,1234,1234],
"c": [1234,1234,1234,1234,1234,1234,1234],
"v": [1234,1234,1234,1234,1234,1234,1234]
},
"M1": {
"t": [1234,1234,1234,1234,1234,1234,1234],
"o": [1234,1234,1234,1234,1234,1234,1234],
"h": [1234,1234,1234,1234,1234,1234,1234],
"l": [1234,1234,1234,1234,1234,1234,1234],
"c": [1234,1234,1234,1234,1234,1234,1234],
"v": [1234,1234,1234,1234,1234,1234,1234]
}
},
"AUDCHF": {
"MN": {
"t": [1234,1234,1234,1234,1234,1234,1234],
"o": [1234,1234,1234,1234,1234,1234,1234],
"h": [1234,1234,1234,1234,1234,1234,1234],
"l": [1234,1234,1234,1234,1234,1234,1234],
"c": [1234,1234,1234,1234,1234,1234,1234],
"v": [1234,1234,1234,1234,1234,1234,1234]
},
"W1": {
"t": [1234,1234,1234,1234,1234,1234,1234],
"o": [1234,1234,1234,1234,1234,1234,1234],
"h": [1234,1234,1234,1234,1234,1234,1234],
"l": [1234,1234,1234,1234,1234,1234,1234],
"c": [1234,1234,1234,1234,1234,1234,1234],
"v": [1234,1234,1234,1234,1234,1234,1234]
},
"D1": {
"t": [1234,1234,1234,1234,1234,1234,1234],
"o": [1234,1234,1234,1234,1234,1234,1234],
"h": [1234,1234,1234,1234,1234,1234,1234],
"l": [1234,1234,1234,1234,1234,1234,1234],
"c": [1234,1234,1234,1234,1234,1234,1234],
"v": [1234,1234,1234,1234,1234,1234,1234]
},
"H4": {
"t": [1234,1234,1234,1234,1234,1234,1234],
"o": [1234,1234,1234,1234,1234,1234,1234],
"h": [1234,1234,1234,1234,1234,1234,1234],
"l": [1234,1234,1234,1234,1234,1234,1234],
"c": [1234,1234,1234,1234,1234,1234,1234],
"v": [1234,1234,1234,1234,1234,1234,1234]
},
"H1": {
"t": [1234,1234,1234,1234,1234,1234,1234],
"o": [1234,1234,1234,1234,1234,1234,1234],
"h": [1234,1234,1234,1234,1234,1234,1234],
"l": [1234,1234,1234,1234,1234,1234,1234],
"c": [1234,1234,1234,1234,1234,1234,1234],
"v": [1234,1234,1234,1234,1234,1234,1234]
},
"M30": {
"t": [1234,1234,1234,1234,1234,1234,1234],
"o": [1234,1234,1234,1234,1234,1234,1234],
"h": [1234,1234,1234,1234,1234,1234,1234],
"l": [1234,1234,1234,1234,1234,1234,1234],
"c": [1234,1234,1234,1234,1234,1234,1234],
"v": [1234,1234,1234,1234,1234,1234,1234]
},
"M15": {
"t": [1234,1234,1234,1234,1234,1234,1234],
"o": [1234,1234,1234,1234,1234,1234,1234],
"h": [1234,1234,1234,1234,1234,1234,1234],
"l": [1234,1234,1234,1234,1234,1234,1234],
"c": [1234,1234,1234,1234,1234,1234,1234],
"v": [1234,1234,1234,1234,1234,1234,1234]
},
"M5": {
"t": [1234,1234,1234,1234,1234,1234,1234],
"o": [1234,1234,1234,1234,1234,1234,1234],
"h": [1234,1234,1234,1234,1234,1234,1234],
"l": [1234,1234,1234,1234,1234,1234,1234],
"c": [1234,1234,1234,1234,1234,1234,1234],
"v": [1234,1234,1234,1234,1234,1234,1234]
},
"M1": {
"t": [1234,1234,1234,1234,1234,1234,1234],
"o": [1234,1234,1234,1234,1234,1234,1234],
"h": [1234,1234,1234,1234,1234,1234,1234],
"l": [1234,1234,1234,1234,1234,1234,1234],
"c": [1234,1234,1234,1234,1234,1234,1234],
"v": [1234,1234,1234,1234,1234,1234,1234]
}
}
}]
基本上,我需要更新old_data。new_data.json.但是这些新数据应该添加到old_data的开头。json,所以最终的json文件"data_updated. json"。json"看起来像这样:
[{"AUDCAD" : {
"MN": {
//New_data | Old_data
"t":[1435708800,1234,1234,1234,1234],
"o":[0.9652,1234,1234,1234,1234],
"h":[0.9653,1234,1234,1234,1234],
"l":[0.94,1234,1234,1234,1234],
"c":[0.9508,1234,1234,1234,1234],
"v":[4040241,1234,1234,1234,1234]
}
},
....
}]
要执行此操作,我在array
的每个位置上使用array_unshift
,我的代码看起来像这样:
//First I open the json files using jsonDB.class.php of Straussn's
$new_data = $db->selectAll("new_data.json");
$old_data = $db->selectAll("old_data.json");
foreach ($new_data as $j) {
array_unshift($old_data[0]["AUDCAD"]["M1"]['t'], $j["AUDCAD"]["M1"]["t"][0]);
array_unshift($old_data[0]["AUDCAD"]["M1"]['o'], $j["AUDCAD"]["M1"]["o"][0]);
array_unshift($old_data[0]["AUDCAD"]["M1"]['h'], $j["AUDCAD"]["M1"]["h"][0]);
array_unshift($old_data[0]["AUDCAD"]["M1"]['l'], $j["AUDCAD"]["M1"]["l"][0]);
array_unshift($old_data[0]["AUDCAD"]["M1"]['c'], $j["AUDCAD"]["M1"]["c"][0]);
array_unshift($old_data[0]["AUDCAD"]["M1"]['v'], $j["AUDCAD"]["M1"]["v"][0]);
//I repeat this spagheti code for each symbol "AUD / ..." each time "M1,M5, M15..." and each data "t, o, h, l, c, v".
//I must repeat this 200000 times.
}
我知道这是完全荒谬的编写/重复+20万行代码来执行这个简单的动作,但我是编程初学者,我找不到一个实用的方法来执行这个动作。也许你可以帮助我做些什么来优化这个动作,学习更多和改进我的代码。
感谢大家的宝贵时间。
你试过了吗,
array_merge_recursive( $newdata, $olddata );
或
array_merge_recursive( $olddata, $newdata );
先有鸡还是先有蛋,我懒得弄清楚。
array_slice可以满足您的要求。
array_splice( $data, 0, 0, $newData);
这将更新$data
并在位置0插入$newData
。