PHP删除除最小值以外的具有相同键的项

2w2cym1i  于 2023-05-16  发布在  PHP
关注(0)|答案(2)|浏览(239)

我有下面的数组,我想从数组中删除元素得到了相同的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应该是数组中的唯一。

rnmwe5a2

rnmwe5a21#

使用foreach循环:

$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);

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;
    },
    []
  )
);

使用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
  ]
];

...结果将是两个版本:

Array
(
    [0] => Array
        (
            [hash] => 546
            [sender] => 139810
            [amount] => 0.015
            [time] => 1683896236
        )

    [1] => Array
        (
            [hash] => 1092
            [sender] => 279620
            [amount] => 0.6666
            [time] => 1683896237
        )

)
eh57zj3b

eh57zj3b2#

// 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);

相关问题