I keep getting duplicate entries (4 to be exact) when using stristr to filter a JSON by keywords. An example of my code is as follows:
$keywords = array('small','medium','large');
foreach($keywords as $keyword) {
foreach ($data as &posts) {
if(stristr($posts['message'],$keyword) !== FALSE ) {
print_r($posts);
}
}
}
When I do the following there are zero duplicates:
foreach ($data as &posts) {
print_r($posts);
}
I have tried array_uniq
but with no success. Can someone please point me in the right direction? Is there a better way to sort JSON?
Do the duplicate posts match more than one keyword? If so, they'll be printed for each match.
If you want to print each post only once per match, loop through the posts first, and quit checking a post when a keyword match is found:
foreach ($data as &posts) {
foreach($keywords as $keyword) {
if(stristr($posts['message'],$keyword) !== FALSE ) {
print_r($posts);
break;
}
}
}