Array_filter in the context of an object, with private callback

元气小坏坏 提交于 2020-01-01 07:42:05

问题


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?


回答1:


Yes:

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

See the documentation for the callback type.



来源:https://stackoverflow.com/questions/8239510/array-filter-in-the-context-of-an-object-with-private-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!