如何删除数组中小于X的整数?

发布于 2024-08-29 05:18:04 字数 62 浏览 2 评论 0原文

我有一个包含从 0 到 100 的整数的数组。我希望删除小于数字 X 的整数并保留等于或大于数字 X 的整数。

I have an array with integers of values from 0 to 100. I wish to remove integers that are less than number X and keep the ones that are equal or greater than number X.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘年祭陌 2024-09-05 05:18:04

使用笨重的 create_function 有点难看,但很简单:

$filtered = array_filter($array, create_function('$x', 'return $x >= $y;'));

对于 PHP >= 5.3:

$filtered = array_filter($array, function ($x) { return $x >= $y; });

$y 设置为您想要的任何值。

A little ugly using the clunky create_function, but straight forward:

$filtered = array_filter($array, create_function('$x', 'return $x >= $y;'));

For PHP >= 5.3:

$filtered = array_filter($array, function ($x) { return $x >= $y; });

Set $y to whatever you want.

当梦初醒 2024-09-05 05:18:04

比生成太大的数组然后将其缩小到一定大小更聪明,我建议从一开始就只生成您想要的数组。

range() 将为您完成这项工作,而无需使用匿名函数调用迭代条件。

代码:(演示)

$rand=rand(0,100);  // This is your X randomly generated

echo $rand,"\n";

$array=range($rand,100);  // generate an array with elements from X to 100 (inclusive)

var_export($array);

潜在输出:

98
array (
  0 => 98,
  1 => 99,
  2 => 100,
)

或者,如果您确实想要修改输入您已经生成的数组,然后假设您有一个索引数组,您可以使用 array_slice() 来删除元素,使用 X 来定位起始偏移量并可选择保留索引/键。

代码:(演示)

$array=range(0,100);

$rand=rand(0,100);  // This is your X randomly generated
echo $rand,"\n";

var_export(array_slice($array,$rand));  // reindex the output array

echo "\n";

var_export(array_slice($array,$rand,NULL,true));  // preserve original indexes

潜在输出:

95
array (
  0 => 95,
  1 => 96,
  2 => 97,
  3 => 98,
  4 => 99,
  5 => 100,
)
array (
  95 => 95,
  96 => 96,
  97 => 97,
  98 => 98,
  99 => 99,
  100 => 100,
)

Smarter than generating an array that is too big then cutting it down to size, I recommend only generating exactly what you want from the very start.

range() will do this job for you without the bother of an anonymous function call iterating a condition.

Code: (Demo)

$rand=rand(0,100);  // This is your X randomly generated

echo $rand,"\n";

$array=range($rand,100);  // generate an array with elements from X to 100 (inclusive)

var_export($array);

Potential Output:

98
array (
  0 => 98,
  1 => 99,
  2 => 100,
)

Alternatively, if you truly, truly want to modify the input array that you have already generated, then assuming you have an indexed array you can use array_slice() to remove elements using X to target the starting offset and optionally preserve the indexes/keys.

Code: (Demo)

$array=range(0,100);

$rand=rand(0,100);  // This is your X randomly generated
echo $rand,"\n";

var_export(array_slice($array,$rand));  // reindex the output array

echo "\n";

var_export(array_slice($array,$rand,NULL,true));  // preserve original indexes

Potential Output:

95
array (
  0 => 95,
  1 => 96,
  2 => 97,
  3 => 98,
  4 => 99,
  5 => 100,
)
array (
  95 => 95,
  96 => 96,
  97 => 97,
  98 => 98,
  99 => 99,
  100 => 100,
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文