NodeList is only array like. Removing items using foreach() has undesired results.

@return NodeList
173126
public function children(): NodeList {
        $results = $this->collection()->reduce(function($carry, $node) {
            return $carry->merge(
                $node->findXPath('child::*')
            );
        }, $this->newNodeList());

        return $results;
    }
846199
public function remove()
    {
        unset($this->_nodes[$this->_pointer]);
        $this->_nodes = array_values($this->_nodes);
        return $this;
    }
953366
public function getNodes()
    {
        if (is_null($this->_nodes)) {
            $this->_nodes = new FileSystemList(
                $this->details->getRealPath()
            );
        }
        return $this->_nodes;
    }
79577
public NodeList nodelist() throws javax.xml.transform.TransformerException
  {

    error(XPATHErrorResources.ER_CANT_CONVERT_TO_NODELIST,
          new Object[]{ getTypeString() });  //"Can not convert "+getTypeString()+" to a NodeList!");

    return null;
  }
820567
private ArrayList<E> toArrayList() {
        ArrayList<E> list = new ArrayList<>();
        for (Node<E> p = first(); p != null; p = succ(p)) {
            E item = p.item;
            if (item != null)
                list.add(item);
        }
        return list;
    }
384546
public static List<Element> toElementList(NodeList nodeList) {
		List<Element> elements = new ArrayList<Element>();
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node node = nodeList.item(i);
			if (node instanceof Element) {
				elements.add((Element) node);
			}
		}
		return elements;
	}
385766
public function newNodeList(iterable $nodes = null): NodeList {

        if (!is_iterable($nodes)) {
            if (!is_null($nodes)) {
                $nodes = [$nodes];
            } else {
                $nodes = [];
            }
        }

        return new NodeList($this->document(), $nodes);
    }
231596
public static function DOMNodeListToArray(DOMNodeList $nodeList, $associative = false, $attributes = false)
    {
        $nodes = array();

        foreach ($nodeList as $node) {
            $nodes[] = static::DOMNodeValue($node, $associative, $attributes);
        }

        return $nodes;
    }
23654
public List<PlanNode> removeAllChildren() {
        if (this.children.isEmpty()) {
            return new ArrayList<PlanNode>(0);
        }
        List<PlanNode> copyOfChildren = new ArrayList<PlanNode>(this.children);
        for (Iterator<PlanNode> childIter = this.children.iterator(); childIter.hasNext();) {
            PlanNode child = childIter.next();
            childIter.remove();
            child.parent = null;
        }
        return copyOfChildren;
    }
1102365
private function convertNodeListToArray(DOMNodeList $nodeList)
    {
      $array = array();

      if(!is_null($nodeList) && $nodeList->length > 0)
      {
         foreach($nodeList as $node)
           array_push($array, $node);
      }

      return $array;
    }
267650
public function siblings($selector = null): NodeList {
        $results = $this->collection()->reduce(function($carry, $node) use ($selector) {
            return $carry->merge(
                $node->precedingAll($selector)->merge(
                    $node->followingAll($selector)
                )
            );
        }, $this->newNodeList());

        return $results;
    }
968446
protected function _remove()
	{
    $_tagsArray = array();
		foreach ($this->_tagsArray as $key => $value) {
			if (!in_array($value['tag'], $this->getRemoveTags())) {
				$_tagsArray[$value['tag']] = $value;
			}
		}
		$this->_tagsArray = array();
		$this->_tagsArray = $_tagsArray;
		return $this->_tagsArray;
	}
260968
public function siblings()
    {
        if (!$this->nodes) {
            throw new \InvalidArgumentException('The current node list is empty.');
        }

        return $this->createSubCrawler($this->sibling($this->getNode(0)->parentNode->firstChild));
    }
771107
public NodeList convertToNodeset()
  {

    if (m_obj instanceof NodeList)
      return (NodeList) m_obj;
    else
      return new org.apache.xml.dtm.ref.DTMNodeList(asNodeIterator());
  }
480455
private function _foreach() {
    $node = new ForeachNode();
    $this->mustMatch(T_FOREACH, $node);
    $this->mustMatch('(', $node, 'openParen');
    $node->addChild($this->expr(), 'onEach');
    $this->mustMatch(T_AS, $node);
    $value = $this->foreachVariable();
    if ($this->currentType === T_DOUBLE_ARROW) {
      $node->addChild($value, 'key');
      $this->mustMatch(T_DOUBLE_ARROW, $node);
      $node->addChild($this->foreachVariable(), 'value');
    }
    else {
      $node->addChild($value, 'value');
    }
    $this->mustMatch(')', $node, 'closeParen', FALSE, TRUE);
    if ($this->tryMatch(':', $node, NULL, FALSE, TRUE)) {
      $node->addChild($this->innerStatementListNode(T_ENDFOREACH), 'body');
      $this->mustMatch(T_ENDFOREACH, $node);
      $this->endStatement($node);
      return $node;
    }
    else {
      $node->addChild($this->statement(), 'body');
      return $node;
    }
  }
761044
public function toList()
	{
		$list = array();
		$nodes = $this->getNodes();
		foreach( $nodes as $source )
		{
			$sublist = array();
			foreach( $nodes as $target )
			{
				if( $this->isEdge( $source, $target ) )
					$sublist[$target->getNodeName()] = $this->getEdgeValue( $source, $target );
			}
			$list [$source->getNodeName()] = $sublist;
		}
		return $list;
	}
369613
public function clear() {
    foreach ($this->nodes as $node) {
      if ($node instanceof ParentNode) {
        $node->clear();
      }
    }
    return $this;
  }