First tip is to avoid the use of the unset
unless necessary, as you lose the original value of your variable and this can complicate the debugging process during maintenance - you cannot compare input/output because you modify one to get the other. What you want to do is filter the values of array, so we already have a function to use: array_filter
. Each value of array is a set of words separated by semicolons, so we have to separate them into a list of words:
$result = array_filter($text, function ($setence) use ($element) {
return !in_array($element, explode(';', $setence));
});
See working on Repl.it | Ideone
With that, the result would be:
Array
(
[1] => palavras;sim
)
But when the word is sought "nao"
, without the characters around the word, which should not belong to it since they are separators.
If you want to avoid memory allocation that function explode
intrinsically will do to store the substrings in memory, you can utilize the strpos
(or mb_strpos
if working with multi bytes) treating the situations where the word can start or end with the searched word by adding the separator character before and after the text:
$result = array_filter($text, function ($setence) use ($element) {
return strpos(";{$setence};", ";{$element};") === false;
});
Producing the same result.
For this example would all values that have the "no" be excluded? That is, the result would be
['palavras;sim']
?– Woss
Good afternoon. What do you need this for?
– Wallace Maxters
The ones that should remain are the ones that have ;yes; inside the array. I need to clean the pdf I get from Tran and turn into CSV, but I only need the arrays that have yes to update the database.
– Marcos Seixas