2

I have an array that looks like this:

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

I would like to search for the pattern *google*. Not sure what function to use to do this. in_array doesn't seem to like regex or searching multiple arrays.

2
  • 1
    What are you looking to return after you find this pattern? The number of occurences of this pattern? The keys of the arrays containing this pattern? Or something else altogether? Commented Jul 6, 2011 at 20:46
  • Whether the pattern exist at all, true or false, boolean. Commented Jul 6, 2011 at 22:59

6 Answers 6

1

I would use preg_grep:

http://php.net/manual/en/function.preg-grep.php

Sign up to request clarification or add additional context in comments.

Comments

0

You can use the array_map function, which maps a callback (a function name, a method name as an array of the object and the method name, or a lambda) to every element in an array.

If you'd like me to elaborate, just ask. :)

Comments

0
$filtered = array_filter($array, function ($value) { return strpos($value['host'], 'google') !== false; });

$filtered will contain all entries where the host contains "google". Requires PHP 5.3. Maybe use this with preg_grep instead of strpos as suggested by @Aaron Ray if you're interested in any field, not just the host.


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

This replaces all values with true or false, based on whether it contains the search term. Not sure what you'd use that for though... :)


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

This returns one boolean value whether the value exists at all in the whole array.

Comments

0

I think I've found a much simpler way using serialize().

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

Comments

0

I would loop and use strpos()

Here is a note from php.net that can be found on preg_match() page.

Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster.

4 Comments

Sure can. strpos checks a string(haystack) for an occurrence of another string(needle) and will return the position of string(needle) if it is found or false if it is not found. Using a foreach you can loop through your arrays and check the strings. If you are only looking to find one occurrence in a set then you can break out of the loop after finding the first one. The structure and the end logic will really depend on how you need to manipulate the data.
Thanks for the explanation, but I prefer the serialize() method.
serialize is great and would still work with strpos and as I noted in my answer strpos is faster than preg_match.
Thanks will switch to strpos.
0

array_map and 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
); 

if you do a print_r() on the $example_array, this is how the array structure looks like:

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
        )

)

Functions

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

Results (the values of 0 (zero) are a false match, the values of 1 (one) are a true match):

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
)

9 Comments

You re-wrote the format of my original array. So you're solution does work as is.
I rewrote the array to test, it's still in the same format and will work as is. Did you test it or just down vote me?
I tested it on my code. I don't have an variable called $mx_records, so that's why it broke and that's why I down voted you.
his solution is right. What he wrote is the same structure as you have, just written differently. $arr = array(array(), array()); is the same as $arr = array(); $arr[] = array(); $arr[] = array(); Please consider reading up on basic programming.
in the foreach loop replace the $example_array with your array variable
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.