通过PHP中的对象数组进行过滤

时间:2014-03-10 19:07:48

标签: php arrays oop object

当从数据库中提取数据时,我有一个对象数组,并希望返回'status'字段等于'No Answer'的每个对象:

array (size=21)
0 => 
  object(Customer)[4]
  public 'id' => string '12345' (length=6)
  public 'date' => string '2014-02-16' (length=10)
  public 'first_name' => string 'Jane' (length=5)
  public 'last_name' => string 'Doe' (length=4)
  public 'email' => string 'test@test.com' (length=21)
  public 'phone' => string '01782111444' (length=11)
  public 'status' => string 'No Answer' (length=14)
1 => 
object(Customer)[5]
  public 'id' => string '12346' (length=6)
  public 'date' => string '2014-02-19' (length=10)
  public 'first_name' => string 'John' (length=4)
  public 'last_name' => string 'Smith' (length=9)
  public 'email' => string 'no@no.com' (length=24)
  public 'phone' => string '01606555666' (length=11)
  public 'status' => string 'Left Message' (length=12)

似乎我需要使用array_filter()但似乎无法使其工作

1 个答案:

答案 0 :(得分:2)

就像@Wiseguy评论的那样,你应该在你的查询中这样做,而不是代码。

请参阅array_filter()

array_filter($array, function($i) {
  if(strtolower($i->status) == 'no answer') { return true; }
  else { return false; }
});