Nodes Each - ThomasWeinert/FluentDOM GitHub Wiki

FluentDOM\Nodes::each()

void each(callable $function[, callable|bool|NULL $elementsFilter = NULL]);

Calls the provided function for each node in the list. Optionally a filter argument can be provided. If set to TRUE, only element nodes will be affected.

The first argument of the callbacks will be the node, the second argument the index of the node in the list.

Usage

$fd = new FluentDOM\Nodes(
  '<items><item/><item/><item/></items>'
);
echo $fd
  ->find('//item')
  ->each(
    function($node, $index) {
      $node['index'] = $index;
    }
  );

Output

<?xml version="1.0"?>
<items><item index="0"/><item index="1"/><item index="2"/></items>
⚠️ **GitHub.com Fallback** ⚠️