-4

I have the following function and it's objective is to filter in the array tree those elements what are not conform to the search index and eliminate theme. I can get this function to bring the desired results.

public function negativeKeywordsFilter($products, $negative_keywords){
  $nk=explode(',',$negative_keywords);
  foreach ($products['productItems'] as $product){
    foreach ($product as $item){
        foreach ($nk as $word){
        if (stripos($item['name'],$word) !== false){
        unset($item);                       
    }

  }
}

}
 return $products;
}

My array looks as follows:

array(
    'page' => '1',
    'items' => '234',
    'items' => array(
        'item' => array(
            0 => array(
                'name' => 'second', 
                'description' => 'some description'
            )
        )
    )
)
)

If name is matching with the descriptions, then the value should be unset.

4
  • 2
    Could you also provide examples of expected inputs and outputs? Commented May 31, 2012 at 14:24
  • 1
    @lgt Is that really what your array looks like? Duplicate keys? Really? Commented May 31, 2012 at 14:38
  • I'm going to put here my output Commented May 31, 2012 at 14:46
  • You know everyday I'm learning new things Commented May 31, 2012 at 17:33

1 Answer 1

2

the problem is you only unset a variable which has a copy of the value, you need to unset the corresponding element in the array.

public function negativeKeywordsFilter($products, $negative_keywords){
  $nk=explode(',',$negative_keywords);
  foreach ($products['productItems'] as $key1 => $product){
    foreach ($product as $key2 => $item){
        foreach ($nk as $word){
        if (stripos($item['name'],$word) !== false){
        unset($products['productItems'][$key1][$key2]);                       
    }

  }
}

}
 return $products;
}
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.