Reputation: 17
I am trying to create a function to get the max match word array value from an extensive array. I do not have expertise in PHP. Does witch PHP function do without looping array?
Example
$word = "My New Scooter is very Good":
$array = [
"Good Scooter"
"lovely very Good Scooter"
"Scooter is very Good and New"
"Scooter is very Good and its My New Scooter"
]
4th Number array has All word, so the function should return "Scooter is very Good and its My New Scooter."
The above is just an example, but the array is extensive.
Upvotes: 0
Views: 85
Reputation: 89557
Except perhaps in the magical world of my little poney, you can't do that without looping.
$sentence = 'My New Scooter is very Good';
$array = [
"Good Scooter",
"lovely very Good Scooter",
"Scooter is very Good and New",
"Scooter is very Good and its My New Scooter"
];
$words = explode(' ', $sentence);
$winner = ['index' => false, 'count' => 0 ];
foreach ($array as $k => $v) {
$common = array_intersect($words, explode(' ', $v));
if ( $winner['count'] < count($common) )
$winner = ['index' => $k, 'count' => count($common)];
if ( $winner['count'] === count($words) )
break;
}
var_dump($winner);
Feel free to build a function to do that with a return
instead of a break
.
Upvotes: 1