我有一系列产品,我需要删除所有引用网络研讨会
的产品我使用的PHP版本是5.2.9
$category->products
示例:
[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
)
在这种情况下,数字8将被保留,但其他两个将被剥离......
有人可以帮忙吗?
答案 0 :(得分:5)
结帐array_filter()。假设你运行PHP 5.3+,这就可以解决问题:
$this->categories = array_filter($this->categories, function ($obj) {
if (stripos($obj->title, 'webinar') !== false) {
return false;
}
return true;
});
对于PHP 5.2:
function filterCategories($obj)
{
if (stripos($obj->title, 'webinar') !== false) {
return false;
}
return true;
}
$this->categories = array_filter($this->categories, 'filterCategories');
答案 1 :(得分:3)
你可以尝试
$category->products = array_filter($category->products, function ($v) {
return stripos($v->title, "webinar") === false;
});
答案 2 :(得分:1)
您可以使用array_filter方法。 http://php.net/manual/en/function.array-filter.php
function stripWebinar($el) {
return (substr_count($el->title, 'Webinar')!=0);
}
array_filter($category->products, "stripWebinar")