I want to check if a value in a multidimensional array is the same by a given percent.
For example this is my array:
$shop = array( array( Title => "rose",
Price => 1.25,
Number => 15
),
array( Title => "daisy",
Price => 0.75,
Number => 25,
),
array( Title => "orchid",
Price => 1.15,
Number => 7
)
);
and if a given value (say “testorchid”) is 55% the same as a value in the multidimensional array. Return the corresponding value in the multidimensional array and the procent that is the same.
So in this case. If i check with “testorchid”, it returns “orchid” and 55.56 procent.
A bit like similar_text(): works
I got a function that can check if given value (needle) is the same as a value in a multidimensional array:
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;
}
But it only returns true when a value is exactly the same. Not the corresponding value in the multidimensional array and not the percent of the value thats the same.
I want say something like this: If “orchidtest” is > 60% the same as a [“title”] in the multidimensional array, then give that value en the percent.
Gives you an array
$similarsorted by the most similar, and you can specify a threshold to cut off useless values.output of
print_r($similar):Notice that daisy has not been returned because it’s similarity fell below the threshold. For reference it’s similarity is 13.3333333r