Search all keys in a multidimensional PHP array

I want to search for all keys inside a multidimensional array for a specific string. I just need to figure out if its present, nothing more. I want to know if the visitor's IP address is present in any of the arrays.

Is there a php function or method that I can use for this, each of which I tried to always return false. (in_array, array_search, array_filter)

I was hoping to avoid a loop through each key and many values.

Array Example

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

thank

+2
source share
3 answers

Unable to escape 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*/}
0
source

I can imagine how you would not need a loop (at least on your own):

$term = preg_quote('109.148.183.1', '~'); // lets make sure it 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,
    ),
);

(21, 22 23), 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
        )
)
+1
$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 can write your own recursive function and avoid using a global one. This is the most direct way.

0
source

All Articles