0

I have an array that contains words like 'free advice', 'consulting', 'ask for free' etc. And I want to remove elements that contain the word 'free'. I have the following codes but the output is blank.. Can anyone help me? Thank you in advance!

foreach ($myarray as $value){ 
   // remove words containing 'free' 
   if (strpos($value, 'free') !== false) {
             unset($myarray[$value]);
   }
} 

1
  • 1
    You say the "output is blank." The posted code should not generate any output. What output were you expecting? Commented Jun 19, 2020 at 2:57

1 Answer 1

3

Arrays have keys and values. You're looking at the values, but trying to unset keys. Unless they match (i.e. your array looks like ['test' => 'test', 'freedom' => 'freedom']), this isn't what you want. Try this:

foreach ($myarray as $key => $value){ 
    // remove words containing 'free' 
    if (strpos($value, 'free') !== false) {
        unset($myarray[$key]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.