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
);
если вы выполняете print_r () для $ example_array, вот как выглядит структура массива:
echo print_r($example_array,true)."\n";
Вывод:
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
)