Remove elements from an array where they meet certain criteria in PHP

I have an array of products and I need to remove all of them that have a link to the webinar

The used version of PHP is 5.2.9

$category->products 

Example:

  [6] => stdClass Object ( [pageName] => another_title_webinar [title] => Another Webinar Title ) [7] => stdClass Object ( [pageName] => support_webinar [title] => Support Webinar ) [8] => stdClass Object ( [pageName] => support [title] => Support ) 

In this case, the number 8 will be left, but the other two will be divided ...

Can anyone help?

+4
source share
3 answers

Check out array_filter () . Assuming you are running PHP 5.3+, this could do the trick:

 $this->categories = array_filter($this->categories, function ($obj) { if (stripos($obj->title, 'webinar') !== false) { return false; } return true; }); 

For PHP 5.2:

 function filterCategories($obj) { if (stripos($obj->title, 'webinar') !== false) { return false; } return true; } $this->categories = array_filter($this->categories, 'filterCategories'); 
+5
source

You can try

 $category->products = array_filter($category->products, function ($v) { return stripos($v->title, "webinar") === false; }); 

Simple online demo

+3
source

You can use the array_filter method. http://php.net/manual/en/function.array-filter.php

 function stripWebinar($el) { return (substr_count($el->title, 'Webinar')!=0); } array_filter($category->products, "stripWebinar") 
+1
source

All Articles