I'd like to make a website where users can find recipes by ingredients. How do I check how much an array matches another in percent?
To be more specific:
Pizza 1 contains cheese, tomato, ham, pineapple and beef.
Pizza 2 contains cheese, tomato, bacon, chili and salad
Pizza 3 tomato, salad and pineapple
If I am looking for a pizza, that contains cheese and tomato, then 1 and 2 matches with 100%, while 3 matches 50%. If I am looking for a pizza, that contains tomato, all will match 100%. If I am looking for a pizza, that contains cheese, tomato and ham, then pizza 1 will match 100%, pizza 2 66,67% and pizza 3 33,33%. That'll be easy to make.
But what if I am looking for a pizza, that contains cheese, tomato, ham, bacon, beef and pineapple - that'll be more ingredients in my wishlist than any of the pizza contains. Pizza 1 would match most, pizza 3 would match less. But how much would each pizza match in percent? More important: how to code that in PHP?
array_intersect()
? array_diff()
? An combination? Something else?
And what, if it gets more complicated: I want a pizza that contains cheese, ham and bacon, but no pineapple. How would I make such?
I imagine, I am having a few arrays for the pizzas:
$pizza[0] = ['cheese', 'tomato', 'ham', 'beef', 'pineapple'];
$pizza[1] = ['cheese', 'tomato', 'bacon', 'chili', 'salad'];
$pizza[2] = ['tomato', 'salad', 'pineapple'];
Additionally, I am having an $included_wish and an $excluded_wish:
$included_wish = ['cheese', 'ham', 'bacon'];
$excluded_wish = ['pineapple'];