php数字数组选择大于一个数字且小于另一个数字的值并将其保存到一个新数组中 [英] php numeric array select values greater than a number and lower than another and save it to a new array

查看:38
本文介绍了php数字数组选择大于一个数字且小于另一个数字的值并将其保存到一个新数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下数字的数组(动态创建的)

I have one array(dynamically created) that contains the following numbers

$numbers = array (200, 400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000, 12000, 14000, 15000, 16000, 18000, 20000, 21000, 24000, 25000, 27000, 30000, 35000, 40000, 45000, 50000, 60000, 70000, 75000, 80000, 90000, 100000, 105000, 120000, 135000, 140000, 150000, 160000, 180000, 200000, 250000, 300000, 350000, 400000, 450000, 500000, 600000, 700000, 800000, 900000, 1000000)

我想通过 >= 和 <= 创建新数组(过滤),例如新数组包含大于或等于 (>=) 大于 800 且小于或等于 (<=) 小于 1600 的数字

I want to create new array (filtered) by >= and <= for example the new array to contains numbers greater or equal(>=) than 800 and lower or equal(<=) than 1600

New Array
(
    [0] => 800
    [1] => 1000
    [2] => 1200
    [3] => 1400
    [4] => 1600
)

可以不使用 foreach 吗?

is that possible without using foreach?

推荐答案

$min = 800;
$max = 1200;
$newNumbers = array_filter(
    $numbers,
    function ($value) use($min,$max) {
        return ($value >= $min && $value <= $max);
    }
);

这篇关于php数字数组选择大于一个数字且小于另一个数字的值并将其保存到一个新数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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