<?php
function get_genetic_num($parent, $child)
{
$count = 0;
for ($i=0; $i< strlen($child); $i++)
{
if (strstr($parent, $child[$i]))
$count++;
}
return $count;
}
$str1 = "003";
$str2 = "085";
echo get_genetic_num($str1, $str2)."\n";
$str3 = "851";
echo get_genetic_num($str2, $str3)."\n";
$str4 = "999";
echo get_genetic_num($str3, $str4)."\n";
?>
输出:
1
2
0
function get_genetic_num($parent, $child)
{
$count = 0;
for ($i=0; $i< strlen($child); $i++)
{
if (strstr($parent, $child[$i]))
$count++;
}
return $count;
}
$str1 = "003";
$str2 = "085";
echo get_genetic_num($str1, $str2)."\n";
$str3 = "851";
echo get_genetic_num($str2, $str3)."\n";
$str4 = "999";
echo get_genetic_num($str3, $str4)."\n";
?>
输出:
1
2
0
<?php
function get_genetic_num($parent, $child)
{
$count = 0;
$h = array();
for ($i=0; $i< strlen($child); $i++)
{
if (!$h[$child[$i]] && strstr($parent, $child[$i]))
{
$h[$child[$i]] = 1;
$count++;
}
}
return $count;
}
$str1 = "003";
$str2 = "085";
echo get_genetic_num($str1, $str2)."\n";
$str3 = "851";
echo get_genetic_num($str2, $str3)."\n";
$str4 = "999";
echo get_genetic_num($str3, $str4)."\n";
?>
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result_array = array_intersect_assoc($array1, $array2);
?> Array
(
[a] => green
)直接这个函数就可以了!希望楼主会用,很简单,就是把上面的两字符串分开成数据应用这个函数就OK了!
$str1 = "003";
$str2 = "085";
$str3 = "851";
function renum($arrr,$arrr1)
{
return array_intersect($arrr,$arrr1);
}
$arr=str_split($str1,1);
$arr1=str_split($str2,1);
$arr2=str_split($str3,1);
echo count(array_unique(renum($arr,$arr1)))."<br>";
echo count(array_unique(renum($arr2,$arr1)));
?>呵呵,连循环都不用
$str1 = "003";
$str2 = "085";
$str3 = "851";
function renum($arrr,$arrr1)
{
return array_unique(array_intersect($arrr,$arrr1));
}
$arr=str_split($str1,1);
$arr1=str_split($str2,1);
$arr2=str_split($str3,1);
echo count(renum($arr,$arr1))."<br>";
echo count(renum($arr2,$arr1));
?>