在多维数组 PHP 的所有键中搜索
我想在多维数组中的所有键中搜索特定字符串。我只需要弄清楚它是否存在,仅此而已。我想知道访问者的 IP 是否存在于任何数组中。
有没有我可以用来执行此操作的 php 函数或方法,我尝试过的每个函数或方法总是返回 false。 (in_array、array_search、array_filter)
我希望避免循环遍历每个键和值集。
示例数组
Array
(
[21] => Array
(
[click_id] => 21
[ip_addr] => 109.148.183.1
[dtime] => 2011-04-28 17:56:57
[url_id] => 11
)
[22] => Array
(
[click_id] => 22
[ip_addr] => 109.148.183.1
[dtime] => 2011-04-28 17:57:05
[url_id] => 12
)
[23] => Array
(
[click_id] => 23
[ip_addr] => 109.148.183.1
[dtime] => 2011-04-28 18:42:42
[url_id] => 10
)
)
谢谢
I want to search all keys within a multidimensional array for a specific string. I just need to work out if its present, nothing more. I want to know if an IP of a visitor is present within any of the arrays.
Is there a php function or method I can use to do this, each one I've tried always returns false. (in_array, array_search, array_filter)
I was hoping to avoid looping through each key and set of values.
Example Array
Array
(
[21] => Array
(
[click_id] => 21
[ip_addr] => 109.148.183.1
[dtime] => 2011-04-28 17:56:57
[url_id] => 11
)
[22] => Array
(
[click_id] => 22
[ip_addr] => 109.148.183.1
[dtime] => 2011-04-28 17:57:05
[url_id] => 12
)
[23] => Array
(
[click_id] => 23
[ip_addr] => 109.148.183.1
[dtime] => 2011-04-28 18:42:42
[url_id] => 10
)
)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
永远无法避免循环:-)
Can never avoid the loop :-)
或者,您可以编写自己的递归函数并避免使用全局函数。这只是最直接的方法。
Alternatively you could write your own recursive function and avoid the use of a global. This is just the most direct way.
我可以想象一种您不必循环的方式(至少您自己):
使用您的示例数据:
它正确返回键(
21
,22
和23
),其值为109.148.183.1
:由于这是一个正则表达式,我们能够进行更强大的搜索,例如搜索所有
2011-04-28
秒数为奇数的日期:输出:
I can imagine a way you wouldn't have to loop (at least yourself):
With your example data:
It correctly returns keys (
21
,22
and23
) that have the value109.148.183.1
:And since this is a regular expression we are able to do even more powerful searches, for instance searching for all
2011-04-28
dates that have an odd number of seconds:And the output: