PHP比较两个数组的相似度

//author: [email protected]
//description: 比较两个数组的相似度,选取相似度最长的一组数组
//帮同事写的函数,使用请尊重作者版权
 
function similar($array){
	foreach ($array as $arr){
		$a[] = implode('',$arr);
	}

	$count = count($a);
	for ($i = 0; $i < $count; $i++){
		if ($i == $count-1){
			$j = 0;
		}else{
			$j = $i+1;
		}
		$j_count = $count-1;
		for (;$j < $j_count; $j++){
			similar_text($a[$i],$a[$j],$percent);
			$res[$i.'_'.$j] = $percent;
		}
	}

	arsort($res);
	$keys = explode('_', key($res));
	$result = max($array[$keys[0]], $array[$keys[1]]);
	return $result;
}
 
示例:
$test = array( 
	array(12,11,35), 
	array(2,10,38), 
	array(2,10), 
	array(3,10,38,3,10,38), 
	array(4,11,35,12), 
	array(3,10), 
	array(4,11,35,12,1),
);
 
$res = similar($test);

//————————————————debug——————————————————————————
echo '<pre>';
print_r ($res);
echo '</pre>';
exit();
//————————————————debug——————————————————————————

你可能感兴趣的:(PHP,function)