基本上我有一个这种结构的数组:
阵列:
0 => {
time: "090019"
val: "2.444"
qta: "292"
dir: "up"
param: "S"
}
1=> {
time: "090019"
val: "2.442"
qta: "938"
dir: "dwn"
param: "N"
}
2 => {
time: "090019"
val: "2.442"
qta: "220"
dir: "up"
param: "N"
}
3 => {
time: "100043"
val: "2.44"
qta: "220"
dir: "dwn"
param: "N"
}
..等等等等
n => {
time: "103051"
val: "2.444"
qta: "330"
dir: "dwn"
param: "N"
}
我的目标是获得一个具有相同结构的数组,以精确的方式合并数据。
如果两个或更多的数组值具有相同的时间(时间)和值(val),则它们将是:如果它们具有相同的方向(dir)则被添加,如果它们没有,则被减去(在这种情况下,结果的方向将被减去)是较高价值的(dir)。 param(param)无关紧要。
我对Php很陌生,当我不得不解决这样的事情时,我永远不知道如何处理。
希望足够清楚,任何建议都将受到赞赏。
编辑:
结果数组将如下:
0 => {
time: "090019"
val: "2.444"
qta: "292"
dir: "up"
param: "S"
}
1 => {
time: "090019"
val: "2.442"
qta: "718"
dir: "dwn"
param: "N"
}
2 => {
time: "100043"
val: "2.44"
qta: "220"
dir: "dwn"
param: "N"
}
..
n => {
time: "103051"
val: "2.444"
qta: "330"
dir: "dwn"
param: "N"
}
答案 0 :(得分:0)
这是一个通用答案,您可以使用新的qta执行所需的操作:
$items = array([put your data here]);
for($i=0; $i < count($items); $i++) {
for($j=0; $j < count($items); $j++) {
// don't compare and item to itself
if($i == $j) continue;
// if their times and values are equal
if( $items[$i]["time"] == $items[$j]["time"] && $items[$i]["val"] == $items[$j]["val"]) {
// dirs are the same
if( $items[$i]["dir"] == $items[$j]["dir"] ) {
$newqta = $items[$i]["qta"] + $items[$j]["qta"];
$newdir = $items[$i]["dir"];
}
// dirs are different
else {
$newqta = $items[$i]["qta"] - $items[$j]["qta"];
if( $items[$i]["qta"] > $items[$j]["qta"] ) {
$newdir = $items[$i]["dir"];
}
else {
$newdir = $items[$j]["dir"];
}
}
// do something with $newqta and $newdir
}
}
}
答案 1 :(得分:0)
这是一个有趣的问题,所以这里的东西可能适合你,开箱即用:)
首先,定义您的项目:
$items = array(
array(
"time" => "090019",
"val" => "2.444",
"qta" => "292",
"dir" => "up",
"param" => "S",
),
array(
"time" => "090019",
"val" => "2.442",
"qta" => "938",
"dir" => "dwn",
"param" => "N",
),
array(
"time" => "090019",
"val" => "2.442",
"qta" => "220",
"dir" => "up",
"param" => "N",
),
array(
"time" => "100043",
"val" => "2.44",
"qta" => "220",
"dir" => "dwn",
"param" => "N",
),
);
接下来,迭代items数组一次,根据相同的时间和val将项目组合在一起:
$grouped_items = array();
foreach($items as $item) {
$key = "{$item['time']}-{$item['val']}";
if( empty($grouped_items[$key]) ) $group_items[$key] = array();
$grouped_items[$key][] = $item;
}
此时,您有一个数组,其键等于time-val。其中每个都是一个子数组,其中包含与item-val匹配的所有项的列表(因此,您将相同的时间/值项组合在一起)。最后,您可以创建合并列表:
$final_items = array();
foreach($grouped_items as $item_list) {
$final_items[] = mergeItems($item_list);
}
// merge a list of items with the same time and val
function mergeItems($items) {
$merged_item = array(
"time" => $items[0]['time'],
"val" => $items[0]['val'],
"qta" => 0,
"dir" => null,
"param" => $items[0]['param'],
);
// add or subtract qta values
foreach($items as $item) {
if( $item['dir'] == "up" ) {
$merged_item["qta"] += (int) $item["qta"];
}
else {
$merged_item["qta"] -= (int) $item["qta"];
}
}
// set the final qta and dir value
if( $merged_item["qta"] < 0 ) {
$merged_item["qta"] *= -1;
$merged_item["dir"] = "down";
}
else {
$merged_item["dir"] = "up";
}
return $merged_item;
}
现在$ final_items有你的合并列表。
如果将所有这些粘贴到一个PHP脚本中,它将运行并为您提供所需的输出。如果它不能完全按照您的实际数据进行操作,那么您忘记在问题中提及某些内容,并且可以使用此示例来创建可行的内容:)