I am trying to compare a variable that contains a string of fruits and is stored in a mysql database to an array of fruits. My code looks like this:
$stored_fruits = mysqli_query($this->con, "SELECT fruits FROM fruit_table"); // uploaded fruits: "Apple Orange Pineapple"
$fruit_array = explode(", ", $fruits); // fruits are searched for and turned into an array: ["Apple", "Strawberry", "Banana"]
for($i = 0; $i < count($fruit_array); $i++) {
if($fruit_array[$i] == substr($stored_fruits)) {
echo "fruit match";
}
}
$fruits
is a string of fruits that is taken from what the user types in and $stored_fruits
accesses strings of fruits already in the database. $fruits
is an array that looks like ["Apple", "Pineapple", "Strawberry"]
. $stored_fruits
looks like: "Strawberry Orange Banana"
. I want to compare each element in the array to the uploaded fruits. If there is a match, I want it to echo match. Any ideas? Thanks.