note 37146 deleted from function.domnode-child-nodes by sobak

From: Date: Fri, 25 Nov 2016 05:36:37 +0000
Subject: note 37146 deleted from function.domnode-child-nodes by sobak
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-207637@lists.php.net to get a copy of this message
Note Submitter: zombie(at)artattack(dot)to ---- Here is a handy little function i started using at work and home quite a bit. It takes in an array of nodes, typically from DomNode->child_nodes() as well as a filter argument and returns an array of filtered out nodes. If the $filter arg is a string, then the $boolean argument is used. If boolean is true, then only nodes named $filter will be returned. If boolean is false, then only nodes NOT named $filter will be returend. Additionally, you can specify the filter as an assoc array in the form of $array[$node_name] = true|false and only nodes in the array with a true value are returend. Note that if it is an array, then $boolean is not used. I was planning on having the boolean act as a negation kinda like !in_array(), but it was more for completeness than need so i dropped it. function get_filtered_children($children_array,$filter,$boolean = true){ $total_children = count($children_array); for ($i = 0; $i < $total_children; $i++){ $cur_child = $children_array[$i]; $node_name = $cur_child->node_name(); if (is_array($filter)){ //array in the form of [node_name] = true|false if ($filter[$node_name]) $filtered_children_array[] = $cur_child; } else if(is_string($filter)){ //string hopefully... if ($boolean && $node_name == $filter) $filtered_children_array[] = $cur_child; elseif (!$boolean && $node_name != $filter) $filtered_children_array[] = $cur_child; } else user_error('filter is of type ' . gettype($filter) . '. Please only supply arrays or strings.', E_USER_ERROR); } return $filtered_children_array; } Blaine Garrett Web Master of the Art Attack http://artattack.to

« previous php.notes (#207637) next »