我有下面的数组,我想从数组中删除元素得到了相同的time
,但只保留一个最低的amount
,我知道排序功能,但我不知道如何使代码与删除元素,感谢欣赏这方面的指导和上帝保佑.周末快乐。
array(
array(
"hash" => 0x111,
"sender" => 0x11111,
"amount" => 0.015,
"time" => 1683896236
),
array(
"hash" => 0x222,
"sender" => 0x22222,
"amount" => 0.055,
"time" => 1683896236
),
array(
"hash" => 0x333,
"sender" => 0x33333,
"amount" => 0.075,
"time" => 1683896236
),
array(
"hash" => 0x444,
"sender" => 0x44444,
"amount" => 0.6666,
"time" => 1683896237
)
)
验证码:
我尝试的是:
$this_array = explode("\n", $this_str);
$keep_array = array();
foreach($this_array as $ta) {
$break = explode("###", $ta);
$this_hash = $break[0];
$this_sender = $break[1];
$this_amount = $break[2];
$this_time = $break[3];
if (!array_key_exists($this_time, $keep_array)) {
echo "This time $this_time does not exist \n";
$keep_array[$this_time] = $this_amount;
} else {
if ($this_amount < $keep_array[$this_time]) {
echo "This time $this_time exist \n";
$keep_array[$this_time] == $this_amount;
}
}
}
print_r($keep_array);
我得到的回应是:
Array
(
[1683896236] => 0.01596236
[1683896237] => 0.6666
)
响应应为2个数组
第一个数组键= 1683896236值为0.015(所有相同时间的最小值1683896236)
第二个数组键= 1683896237值为0.6666
我不知道怎么
1.查找所有具有相同“时间”的数组
1.比较它们的“amount”值,只保留“amount”值最低的同一时间的数组元素
1.相同“时间”的较高“量”将从阵列中删除/取消设置
更新了这篇文章中给出的代码我用这样的数据集尝试了它
0x1###0x1###0.143523304620325378###1683911769
0x2###0x2###0.374362###1683911787
0x3###0x3###0.014925###1683911787
0x4###0x4###1.206308057843180009###1683911787
结果将是
Array
(
[0] => Array
(
[hash] => 0x1
[sender] => 0x1
[amount] => 0.143523304620325378
[time] => 1683911769
)
[1] => Array
(
[hash] => 0x3
[sender] => 0x3
[amount] => 0.014925
[time] => 1683911787
)
[2] => Array
(
[hash] => 0x4
[sender] => 0x4
[amount] => 1.206308057843180009
[time] => 1683911787
)
)
1683911787的所有“时间”中预期只有1,但结果显示为2。在reduce之后,N中的1应该是数组中的唯一。
2条答案
按热度按时间rnmwe5a21#
使用foreach循环:
array_reduce:
使用OP的以下测试数据:
...结果将是两个版本:
eh57zj3b2#