PHP有没有办法优化array_unshift超过200000次执行?

时间:2015-07-25 00:52:38

标签: php json multidimensional-array

我有两个包含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]
    }
  }
}]

基本上,我需要使用 new_data.json 中的最新数据更新 old_data.json 文件。

但是这些新数据应该位于 old_data.json 的开头,所以最终的json文件“ data_updated.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_unshift的每个位置使用array,我的代码如下所示:

//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.
}

我知道执行这个简单的操作完全是荒谬的写/重复+200000行代码,但我是编程的初学者,我找不到实际的方法来执行这个动作。 也许你可以帮助我做些什么来优化这个动作,学习更多并改进我的代码。

感谢大家的时间。

2 个答案:

答案 0 :(得分:1)

你试过吗,

 b = driver.find_elements_by_css_selector('input.there.are.several.classes.input-sm')

或者

array_merge_recursive( $newdata, $olddata );

首先是鸡肉或鸡蛋,我是懒惰想出来的。

答案 1 :(得分:0)

array_slice可能会帮到你。

array_splice( $data, 0, 0, $newData);

这将更新$data并在{0}位置插入$newData