0

私が入れた「針」として、多次元配列で値が50%以上同一であるかどうかを確認したい.

多次元配列で値が同一かどうかを確認できる関数を取得しました。

function in_array_r($needle, $haystack, $strict = true) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }
    }

    return false;
    }

しかし、値の特定のパーセンテージが同一である場合、関数をtrueに戻したいです。次のようなものを統合する必要があると思います:similar_text($value1, $value2, $percent);

if {$percent > 50) {
  // do something
}
4

1 に答える 1

2

可能な場合は再帰関数を避けます

function in_array_r($needle, $haystack, $strict = true) {
    $eq = 0;
    $diff = 0;
    for($i=0,$n=count($haystack); $i<$n; $i++){
        for($j=0,$m=count($haystack[$i]); $j<$m; $j++){
            if (($strict && $haystack[$i][$j] === $needle) || $haystack[$i][$j] == $needle){
                $eq++;
            } else {
                $diff++;
            }
        }
    }
    return $eq/($eq+$diff);
}
于 2012-12-07T00:19:15.250 に答える