簡體   English   中英

如何在 PHP 中的多個 arrays 中搜索模式?

[英]How do I search for patterns in multiple arrays in PHP?

我有一個看起來像這樣的數組:

Array (
    [0] => Array (
        [host] => google.com
        [type] => NS
        [target] => ns2.google.com
        [class] => IN [ttl] => 112756
    )
    [1] => Array (
        [host] => google.com
        [type] => NS
        [target] => ns1.google.com
        [class] => IN
        [ttl] => 112756
    )
    [2] => Array (
        [host] => google.com
        [type] => NS
        [target] => ns3.google.com
        [class] => IN
        [ttl] => 112756
    )
    [3] => Array (
        [host] => google.com
        [type] => NS
        [target] => ns4.google.com
        [class] => IN
        [ttl] => 112756
    )
)

我想搜索模式*google* 不確定使用什么 function 來執行此操作。 in_array 似乎不喜歡正則表達式或搜索多個 arrays。

您可以使用array_map function ,它映射一個回調(一個 function 名稱,作為 object 的數組的方法名稱和每個 lambda 元素中的方法名稱)。

如果你想讓我詳細說明,請問。 :)

我會循環並使用strpos()

這是來自 php.net 的注釋,可以在preg_match() 頁面上找到。

如果您只想檢查一個字符串是否包含在另一個字符串中,請不要使用 preg_match()。 請改用 strpos() 或 strstr() ,因為它們會更快。

array_map 和 preg_match

$example_array[] = array(
    'host'      => 'google.com',
    'type'      => 'NS',
    'target'    => 'ns2.google.com',
    'class'     => 'IN',
    'ttl'       => 112756
);

$example_array[] = array(
    'host'      => 'google.com',
    'type'      => 'NS',
    'target'    => 'ns1.google.com',
    'class'     => 'IN',
    'ttl'       => 112756
); 

$example_array[] = array(
    'host'      => 'yahoo.com',
    'type'      => 'NS',
    'target'    => 'ns1.yahoo.com',
    'class'     => 'IN',
    'ttl'       => 112756
); 

如果您在 $example_array 上執行 print_r(),則數組結構如下所示:

echo print_r($example_array,true)."\n";

Output:

Array
(
    [0] => Array
        (
            [host] => google.com
            [type] => NS
            [target] => ns2.google.com
            [class] => IN
            [ttl] => 112756
        )

    [1] => Array
        (
            [host] => google.com
            [type] => NS
            [target] => ns1.google.com
            [class] => IN
            [ttl] => 112756
        )

    [2] => Array
        (
            [host] => yahoo.com
            [type] => NS
            [target] => ns1.yahoo.com
            [class] => IN
            [ttl] => 112756
        )

)

功能

function look4($haystack, $needle = 'google') {
    return preg_match("/$needle/i", $haystack);
}

// instead of $example_array use your array here
foreach($example_array as $sub_array) {
    $results = array_map("look4", $sub_array);
    print_r($results);
}

結果(0(零)的值是假匹配,1(一)的值是真匹配):

Array
(
    [host] => 1
    [type] => 0
    [target] => 1
    [class] => 0
    [ttl] => 0
)
Array
(
    [host] => 1
    [type] => 0
    [target] => 1
    [class] => 0
    [ttl] => 0
)
Array
(
    [host] => 0
    [type] => 0
    [target] => 0
    [class] => 0
    [ttl] => 0
)
$filtered = array_filter($array, function ($value) { return strpos($value['host'], 'google') !== false; });

$filtered將包含host包含"google"的所有條目。 需要 PHP 5.3。 如果您對任何領域感興趣,而不僅僅是host ,也許可以將它與preg_grep而不是strpos一起使用,如@Aaron Ray 建議的那樣。


array_walk_recursive($array, function ($value) { return strpos($value, 'google') !== false; });

根據是否包含搜索詞,這會將所有值替換為truefalse 不知道你會用它來做什么...... :)


$termExists = array_reduce($array, function ($found, $value) {
    return $found || preg_grep('/google/', $value);
});

這將返回一個 boolean 值,無論該值是否存在於整個數組中。

我想我找到了一種使用serialize()更簡單的方法。

$mymultiarrays = serialize($mymultiarrays);
echo preg_match( '/google/', $mymultiarrays; 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM