如何通过使用php中的regexp在数组中查找数字? [英] how to find only numbers in a array by using regexp in php?

查看:80
本文介绍了如何通过使用php中的regexp在数组中查找数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 $ front-> getRequest()-> getParams()来获取url参数。它们看起来像这样

  Zend_Debug :: dump($ front-> getRequest()-> getParams()); 

array(4){
[ id] => string(7) 3532231
[ module] => string(7) test
[ controller] => string(6) index
[ action] => string(5) index
}

我有兴趣通过 preg_match_all 使用类似于([\s0-9])+

由于某种原因我无法隔离该数字。



有可能会有更多的 id 类似于数组中的值,但是 preg_match_all 应该在一个新数组中将它们还给我



有什么想法吗?



谢谢

解决方案

array_filter()是前往此处的方法。

  $ array = array_filter($ array,function($ value){
return preg_match('/ ^ [0-9] + $ /',$ value);
});

您可能还想将preg_match()替换为is_numeric()以提高性能。

  $ array = array_filter($ array,function($ value){
return is_numeric($ value);
});

应该给出相同的结果。


i am using $front->getRequest()->getParams() to get the url params. They look like this

Zend_Debug::dump($front->getRequest()->getParams());

array(4) {
  ["id"] => string(7) "3532231"
  ["module"] => string(7) "test"
  ["controller"] => string(6) "index"
  ["action"] => string(5) "index"
}

i am interested in running this through preg_match_all to get back only the id numbers by using some regexp similar to ([\s0-9])+

for some reason i cant isolate that number.

There is the possibility that there will be more id like values in the array, but the preg_match_all should give them back to me in a new array

any ideas?

thanks

解决方案

array_filter() is the way to go here.

$array = array_filter($array, function($value) {
    return preg_match('/^[0-9]+$/',$value);
});

You might also want to replace the preg_match() with is_numeric() for performance.

$array = array_filter($array, function($value) {
    return is_numeric($value);
});

That should give the same results.

这篇关于如何通过使用php中的regexp在数组中查找数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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