我有下面的数组,我想从得到相同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

我不知道该怎么做

  1. 查找所有具有相同"时间"的数组
  2. 比较它们的"Amount"值,只保留"Amount"最小的同一时间的数组元素
  3. 将从数组中删除/取消设置相同"时间"的较高"量"

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 后数组中的唯一一个.

推荐答案

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

WITH ARRAY_REDUTE:

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

使用来自操作的以下测试数据:

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

)

Php相关问答推荐

获得客户S在WooCommerce为期1年的总支出

在Google云存储对象上从PHP设置缓存控制TTL会更改错误的元数据

如何显示每个分类的当前术语的帖子中包含的前20个标签,不包括标签分类

通过DO LAMP进行身份验证并从邮箱发送邮箱的最简单方式是什么

根据选定的字段值显示或隐藏WooCommerce注册字段

CKEDITOR-如何在PHP中判断提交后的空数据

在带有livewire 3的laravel中使用规则方法时,无法进行实时验证

如何在WordPress REST API中判断应用程序密码

限制联系人页面仅允许在WooCommerce中登录的用户

根据类别的数量折扣更改WooCommerce购物车商品价格

HTTPPost请求在从php脚本调用时返回404,但在从node.js脚本调用时有效.终结点有效

打折时更改 Woocommerce 产品名称

WooCommerce:如果购物车中已存在产品,则增加购物车商品数量

多个产品类别 Select 自定义 WooCommerce 插件设置页面

计算添加到购物车的点击次数并将其显示在 WooCommerce 管理产品列表中

由于 PHP 版本不受支持,如何终止 PHP 脚本?

将 WooCommerce 0 销售价格替换为自定义文本,并保留常规价格删除线

Laravel 模型将日期保存为空

如何限制for循环php中的条目数

Php 的新WeakMap - 枚举曾经被垃圾收集过吗?