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
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
)
)
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*/}
$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.
精彩评论