I have the below array, I would like to delete element from the array which got the same time
but only keep the one with the lowest amount
, I got know of sort function but I not sure how to make the code to work with delete away element, Thanks appreciate guide on this and god bless. happy weekend.
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:
What I tried:
$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);
The response I get was:
Array
(
[1683896236] => 0.01596236
[1683896237] => 0.6666
)
The response expected to be 2 array
First array key = 1683896236
Value is 0.015 (lowest value of all same time 1683896236)
Second array key = 1683896237
Value is 0.6666
I not sure how to
- Find all array that is of same "time"
- Compare their "amount" value , and only keep the array element of the same time with Lowest "amount"
- The higher "amount" of same "time" will be deleted / unset from the array
Updated with codes given on this post
I tried it with a dataset such as this
0x1###0x1###0.143523304620325378###1683911769
0x2###0x2###0.374362###1683911787
0x3###0x3###0.014925###1683911787
0x4###0x4###1.206308057843180009###1683911787
Result will be
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
)
)
Expected only 1 out of all "time" of 1683911787 but result shown 2. 1 out of N should be the only in the array after the reduce.
2
Answers
With a foreach loop:
With array_reduce:
With the following test data from the OP:
… the result will be with both versions: