Can I use array_filter () on an array of classes?
I'm trying to intercept and filter elements from a $ this → _ vars classset array, in a stripped-down version of Smarty (not my choice: |)
This is what I tried 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 seems to be detecting which ones are in the array and which are not, since I replaced the returned data with print. However, nothing is removed from the array. Is there a way to do what I am trying, or am I missing something obvious?
+2
source to share
1 answer
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 are not using 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;
}
+4
source to share