gpt4 book ai didi

php - 从数组中删除符合 PHP 中特定条件的项目

转载 作者:可可西里 更新时间:2023-11-01 13:47:40 24 4
gpt4 key购买 nike

我有一系列产品,我需要删除所有引用网络研讨会的产品

我使用的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 将被保留,但其他两个将被删除......

有人能帮忙吗?

最佳答案

查看 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');

关于php - 从数组中删除符合 PHP 中特定条件的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13912242/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com