使用 array_map 来测试值?
是否可以使用 array_map() 来测试数组的值?我想确保数组的所有元素都是数字。
我已经尝试过这两种方法
$arrays = array(
array(0,1,2,3 )
, array ( 0,1, "a", 5 )
);
foreach ( $arrays as $arr ) {
if ( array_map("is_numeric", $arr) === FALSE ) {
echo "FALSE\n";
} else {
echo "TRUE\n";
}
}
,
$arrays = array(
array(0,1,2,3 )
, array ( 0,1, "a", 5 )
);
foreach ( $arrays as $arr ) {
if ( ( array_map("is_numeric", $arr) ) === FALSE ) {
echo "FALSE\n";
} else {
echo "TRUE\n";
}
}
并且对于这两种方法我都得到了
TRUE
TRUE
这可以完成吗?如果是这样,我做错了什么?
注意:我知道我可以从 foreach 循环中获得我想要的功能。
Is it possible to use array_map() to test values of an array? I want to make sure that all elements of an array are numeric.
I've tried both
$arrays = array(
array(0,1,2,3 )
, array ( 0,1, "a", 5 )
);
foreach ( $arrays as $arr ) {
if ( array_map("is_numeric", $arr) === FALSE ) {
echo "FALSE\n";
} else {
echo "TRUE\n";
}
}
and
$arrays = array(
array(0,1,2,3 )
, array ( 0,1, "a", 5 )
);
foreach ( $arrays as $arr ) {
if ( ( array_map("is_numeric", $arr) ) === FALSE ) {
echo "FALSE\n";
} else {
echo "TRUE\n";
}
}
And for both I get
TRUE
TRUE
Can this be done? If so, what am I doing wrong?
Note: I am aware that I can get my desired functionality from a foreach loop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
array_map 返回一个数组。所以它总是被认为是“真实的”。现在,如果您
array_search
为FALSE,您也许能够获得想要的效果。来自 PHP.net 页面
这意味着当前您有一个每个元素包含 true 或 false 的数组。您需要使用 array_search(false,$array) 来查找是否存在任何 false 值。
array_map returns an array. So it will always be considered 'true'. Now, if you
array_search
for FALSE, you might be able to get the desire effects.From the PHP.net Page
This means that currently you have an array that contains true or false for each element. You would need to use
array_search(false,$array)
to find out if there are any false values.我通常是
array_map()
、array_filter()
等的大力倡导者,但在这种情况下foreach()
将是最好的选择。原因是对于 array_map() 和其他方法,无论如何它都会遍历整个数组。但出于您的目的,您只需要遍历数组,直到遇到is_numeric()
返回 false 的值,据我所知,PHP 中无法突破这些方法。换句话说,如果数组中有 1,000 个项目,并且第 5 个不是数字,则使用
array_map()
仍会检查剩余的 995 个值,即使您已经知道该数组未通过你的测试。但如果您使用 foreach() 并在is_numeric() == false
上break
,那么您只需要检查前五个元素。I'm usually a big advocate of
array_map()
,array_filter()
, etc., but in this caseforeach()
is going to be the best choice. The reason is that witharray_map()
and others it will go through the entire array no matter what. But for your purposes you only need to go through the array until you run into a value for whichis_numeric()
returns false, and as far as I know there's no way in PHP to break out of those methods.In other words, if you have 1,000 items in your array and the 5th one isn't numeric, using
array_map()
will still check the remaining 995 values even though you already know the array doesn't pass your test. But if you use a foreach() instead and have itbreak
onis_numeric() == false
, then you'll only need to check those first five elements.您可以使用过滤器,但最终会得到一些可怕的代码
使用自定义函数会使它好一点,但仍然不完美
但是如果您使用自定义函数 array_reduce 可以工作,但它仍然有一些缺点。
缺点是当它找到它想要的东西时它不会中断,所以上面的功能建议不是很有效。您需要编写一个像这样的函数:
并像这样使用它
You could use filter, but it ends up with a horrible bit of code
Using a custom function makes it a bit better, but still not perfect
But if you were using custom functions array_reduce would work, but it still has some failings.
The failings are that it won't break when it has found what it wants, so the functional suggestions above are not very efficient. You would need to write a function like this:
And use it like so
我的“标准库”中有两个微小但非常有用的函数
在你的例子中,
i have two tiny but extremely useful functions in my "standard library"
in your example
一种更优雅的方法恕我直言:
如果所有值都是数字,则返回 true;如果任何值不是数字,则返回 false。
A more elegant approach IMHO:
This will return true if all the values are numeric and false if any of the values is not numeric.