note 37146 added to function.domnode-child-nodes

From: Date: Tue, 04 Nov 2003 22:17:44 +0000
Subject: note 37146 added to function.domnode-child-nodes
Groups: php.notes 
Request: Send a blank email to php-notes+get-59910@lists.php.net to get a copy of this message
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 ---- Manual Page -- http://www.php.net/manual/en/function.domnode-child-nodes.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+37146 Delete -- http://master.php.net/manage/user-notes.php?action=delete+37146&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+37146&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#59910) next »