skip to Main Content

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

  1. Find all array that is of same "time"
  2. Compare their "amount" value , and only keep the array element of the same time with Lowest "amount"
  3. 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


  1. // build up a result array, indexed by the 'time' value
    $result = [];
    foreach ($a as $v) {
      // either we haven't encountered this time value before
      // or this new amount value is lower than we have stored
      if (!array_key_exists($v['time'], $result) || ($result[$v['time']]['amount'] > $v['amount'])) {
        // add or overwrite entry in the result array
        $result[$v['time']] = $v;
      }
    }
    
    // optional: if you need a simple numeric array (rather than indexed by time value), then this should clean it up
    $result = array_values($result);
    
    Login or Signup to reply.
  2. With a foreach loop:

    $result = [];
    
    foreach ($input as $item) {
      $time = $item['time'];
      if (array_key_exists($time, $result)) {
        if ($item['amount'] < $result[$time]['amount']) {
          $result[$time] = $item;
        }
      } else {
        $result[$time] = $item;
      }
    }
    
    $result = array_values($result);
    

    With array_reduce:

    $result = array_values(
      array_reduce(
        $input,
        static function (array $carry, array $item): array
        {
          $time = $item['time'];
          if (array_key_exists($time, $carry)) {
            if ($item['amount'] < $carry[$time]['amount']) {
              $carry[$time] = $item;
            }
          } else {
            $carry[$time] = $item;
          }
          return $carry;
        },
        []
      )
    );
    

    With the following test data from the OP:

    $input = [
      [
        'hash'   => 0x333,
        'sender' => 0x33333,
        'amount' => 0.075,
        'time'   => 1683896236
      ],
      [
        'hash'   => 0x444,
        'sender' => 0x44444,
        'amount' => 0.6666,
        'time'   => 1683896237
      ],
      [
        'hash'   => 0x111,
        'sender' => 0x11111,
        'amount' => 0.055,
        'time'   => 1683896236
      ],
      [
        'hash'   => 0x222,
        'sender' => 0x22222,
        'amount' => 0.015,
        'time'   => 1683896236
      ]
    ];
    

    … the result will be with both versions:

    Array
    (
        [0] => Array
            (
                [hash] => 546
                [sender] => 139810
                [amount] => 0.015
                [time] => 1683896236
            )
    
        [1] => Array
            (
                [hash] => 1092
                [sender] => 279620
                [amount] => 0.6666
                [time] => 1683896237
            )
    
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search