开发者

Can I use array_filter() on a class array?

I'm attempting to intercept and filter items out of a class set array, $this->_vars, in a stripped down version of Smarty (not my choice :| )

Here's what I've been attempting to use:

Class callback function

private function callback_check($var){
    if(!in_array($var['link_id'], $this->returned_array['items'])) return false;
    else return true;
}

And the array filter itself:

foreach($this->_vars['content']['documents'] as $group_key => $link_groups){
    array_filter($this->_vars['content']['documents'][$group_key]['links'], array(&$this, "callback_check"));
}

Now it does appear to be detecting which ones are in the array and which aren't, as I replaced the returns 开发者_StackOverflow中文版with prints to check. However, nothing is being removed from the array. Is there any way to do what I'm attempting, or have I missed something obvious?


I think you missed something obvious ;)

array_filter() does not filter the array in place, it returns a new, filtered array. Given your code snippet, you do not use the returned array. Try something like this:

foreach($this->_vars['content']['documents'] as $group_key => $link_groups){
    $filtered_array = array_filter($this->_vars['content']['documents'][$group_key]['links'], array(&$this, "callback_check"));
    $this->_vars['content']['documents'][$group_key]['links'] = $filtered_array;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜