/** * Merges two lists of nodes in a single list. * * This function merges two list of nodes in a single list without repeating * nodes. * * @param array $items1 List of DOMNode objects * @param array $items2 List of DOMNode objects * * @return array of DOMNode objects */ public static function mergeNodes($items1, $items2) { $ret = array(); $items = array_merge($items1, $items2); $len = count($items); // retrieves non-repeated elements for ($i = 0; $i < $len; $i++) { $item = $items[$i]; $position = DomHelper::searchNode($item, $items, $i + 1); if ($position === false) { array_push($ret, $item); } } // sorts elements in order of appareance in the document usort($ret, function ($node0, $node1) { $path0 = DomHelper::_getNodePath($node0); $path1 = DomHelper::_getNodePath($node1); $count0 = count($path0); $count1 = count($path1); $len = min($count0, $count1); for ($i = 0; $i < $len; $i++) { if ($path0[$i] != $path1[$i]) { return $path0[$i] > $path1[$i]; } } return $count0 > $count1; }); return $ret; }