i want to create a simple search function with an input field where the user can search messages, stored in the database, by tags.
The problem is that the function returns ALL messages and not the one where the tags match.
Here is my try:
public function getMatches($tagsGiven) {
$mergedTagArray = array();
$messageToTag = array();
$messageRepo = $this->messageRepository->findAll(); // all messages from DB
$messageTagsArray = explode(',', $tagsGiven); // tags given by the user
foreach ($messageRepo as $oMessage) {
$messageTag = $oMessage->getMessageTags(); // get tags from all messages
$storeTagsUnseri = unserialize($messageTag); // unserialize tags given by the user
if (!in_array($messageTagsArray,$storeTagsUnseri)) { //if tags from user matches tags from DB messages
$messageToTag[] = $oMessage->getMessageText(); // get the message text from the DB message and store it in array
}
}
return $messageToTag; // return all message texts matching
}
}
Thank you very much!
Best regards
EDIT
if (array_intersect($messageTagsArray,$storeTagsUnseri)) {}
That did the trick.
Ok as I mantioned in my comment you are trying to see if object is not in array. You can’t do it like that, you have to convert your object to arrat first. So add this to your code if you dont expect multidimensional array:
or just do:
And than do array_intersect()