2012-12-17 39 views
4

符合特定條件我有琳琅滿目的商品,我需要將它們全部刪除其中有一個參考網絡研討會從陣列中刪除項目,他們在PHP

我使用的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

看起來像array_filter()有一個回調的對象頁 –

回答

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'); 
+0

感謝編輯測試 「研討會」 的情況下。 +1 –

+0

葉,看錯了。 :)我認爲它現在已經修復。 – smottt

+0

哇這是一個快速回應 – Andy

3

您可以嘗試

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

Simple Online Demo

+0

我正在使用5.2.9 :-( – Andy

+0

安迪升級最後支持'26-Feb-2009' ...... ......見http://php.net/ChangeLog-5.php ...你是比你想象的更多的問題.... – Baba

+0

感謝您的eval.in鏈接...很值得的書籤不幸我使用5.2.9 – Andy