Array_filter在一个对象的上下文中,带有私有回调 [英] Array_filter in the context of an object, with private callback

查看:232
本文介绍了Array_filter在一个对象的上下文中,带有私有回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用array_filter函数来过滤数组。它提示在水下使用call_user_func,但没有提到如何在类/对象的上下文中使用任何东西。



解释我的目标的一些伪代码:

  class RelatedSearchBlock {
// ...
私有函数get_filtered_docs(){
return array_filter($ this-> get_docs(),'filter_item');
}

private filter_item(){
return($ doc-> somevalue == 123)
}
}

我需要将'filter_item'更改为 array($ this,'filter_item')

$ p

<$ p

$ p> return array_filter($ this-> get_docs(),array($ this,'filter_item'));

请参阅 回调类型的文档


I want to filter an array, using the array_filter function. It hints at using call_user_func under water, but does not mention anything about how to use within the context of a class/object.

Some pseudocode to explain my goal:

class RelatedSearchBlock {
  //...
  private function get_filtered_docs() {
    return array_filter($this->get_docs(), 'filter_item');
  }

  private filter_item() {
    return ($doc->somevalue == 123)
  }
}

Would I need to change 'filter_item' into array($this, 'filter_item') ? Is what I want possible at all?

解决方案

Yes:

return array_filter($this->get_docs(), array($this, 'filter_item'));

See the documentation for the callback type.

这篇关于Array_filter在一个对象的上下文中,带有私有回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆