删除数组中超过 2 个小数位的值 [英] Deleting values with more than 2 decimal place in array

查看:44
本文介绍了删除数组中超过 2 个小数位的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大量值的数组.

I have an array with chunks of values.

$value = array(
              array(3075,15,461.25,16,535.05),
              array(3075,15,461.25808,16,535.05),//to be removed
              array(3075,15,461.2,16,535.05234)//to be removed
              );

我需要删除具有超过 2 个小数位的值的行.我该怎么做?

I need to remove the rows that has values with more than 2 decimal places. How do i do that?

推荐答案

您可以使用 strlen(substr(strrchr($x, "."), 1)) 来计算小数和 array_filter 删除任何超过 2 位小数的数组值

You can use strlen(substr(strrchr($x, "."), 1)) to count the decimals and array_filter to remove any array value with more than 2 decimals

$value = array(
        array(3075,15,461.25,16,535.05),
        array(3075,15,461.25808,16,535.05),//to be removed
        array(3075,15,461.2,16,535.05234)//to be removed
);

$value = array_filter($value,
    function ($v) {
        foreach ($v as $x) {
            if (strlen(substr(strrchr($x, "."), 1)) > 2)
                return false;
        }
        return true;
    });

var_dump($value);

输出

array (size=1)
  0 => 
    array (size=5)
      0 => int 3075
      1 => int 15
      2 => float 461.25
      3 => int 16
      4 => float 535.05

这篇关于删除数组中超过 2 个小数位的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆