在多维数组 PHP 的所有键中搜索

发布于 2024-11-04 10:41:30 字数 833 浏览 0 评论 0原文

我想在多维数组中的所有键中搜索特定字符串。我只需要弄清楚它是否存在,仅此而已。我想知道访问者的 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 技术交流群。

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

发布评论

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

评论(3

时光是把杀猪刀 2024-11-11 10:41:31

永远无法避免循环:-)

function search($array, $searchString){
   foreach($array as $key=>$val){
        if(in_array($searchString, $val)) return true;
   }
   return false;
}

//use it like so:
if(search($array, '109.148.183.1')){/*do something*/}

Can never avoid the loop :-)

function search($array, $searchString){
   foreach($array as $key=>$val){
        if(in_array($searchString, $val)) return true;
   }
   return false;
}

//use it like so:
if(search($array, '109.148.183.1')){/*do something*/}
初心未许 2024-11-11 10:41:31
$matching_keys = array();

function search ($item, $key) {
  global $matching_keys();
  // do your testing here, stuff matches in $matching_keys, or however you wanna do it
}
array_walk_recursive($array, 'search');

或者,您可以编写自己的递归函数并避免使用全局函数。这只是最直接的方法。

$matching_keys = array();

function search ($item, $key) {
  global $matching_keys();
  // do your testing here, stuff matches in $matching_keys, or however you wanna do it
}
array_walk_recursive($array, 'search');

Alternatively you could write your own recursive function and avoid the use of a global. This is just the most direct way.

↙厌世 2024-11-11 10:41:30

我可以想象一种您不必循环的方式(至少您自己):

$term = preg_quote('109.148.183.1', '~'); // lets make sure it's safe
$result = array_map('unserialize', preg_filter('~' . $term . '~', '$0', array_map('serialize', $data)));

echo '<pre>';
print_r($result);
echo '</pre>';

使用您的示例数据:

$data = 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,
    ),
);

它正确返回键(212223),其值为 109.148.183.1

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
        )
)

由于这是一个正则表达式,我们能够进行更强大的搜索,例如搜索所有 2011-04-28秒数为奇数的日期:

$term = '2011-04-28 [0-9]{2}:[0-9]{2}:[0-9][13579]';
$result = array_map('unserialize', preg_filter('~' . $term . '~', '$0', array_map('serialize', $data)));

echo '<pre>';
print_r($result);
echo '</pre>';

输出:

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
        )
)

I can imagine a way you wouldn't have to loop (at least yourself):

$term = preg_quote('109.148.183.1', '~'); // lets make sure it's safe
$result = array_map('unserialize', preg_filter('~' . $term . '~', '$0', array_map('serialize', $data)));

echo '<pre>';
print_r($result);
echo '</pre>';

With your example data:

$data = 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,
    ),
);

It correctly returns keys (21, 22 and 23) that have the value 109.148.183.1:

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
        )
)

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:

$term = '2011-04-28 [0-9]{2}:[0-9]{2}:[0-9][13579]';
$result = array_map('unserialize', preg_filter('~' . $term . '~', '$0', array_map('serialize', $data)));

echo '<pre>';
print_r($result);
echo '</pre>';

And the output:

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
        )
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文