我有下面的数组,我想从得到相同time
的数组中删除元素,但只保留最低amount
的元素,我知道排序函数,但我不确定如何使代码与删除离开的元素一起工作,谢谢这方面的赞赏指南和god 保佑.周末快乐.
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
)
)
Code:个
我try 了什么:
$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
我不知道该怎么做
- 查找所有具有相同"时间"的数组
- 比较它们的"Amount"值,只保留"Amount"最小的同一时间的数组元素
- 将从数组中删除/取消设置相同"时间"的较高"量"
Updated with codes given on this post个 我用这样的数据集进行了try
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.1个N中的1个应该是减go 后数组中的唯一一个.