Content · Web Development · PHP

PHP RecursiveTreeIterator::setPrefixPart() Method: Usage and Examples

PHP RecursiveTreeIterator::setPrefixPart() Method: Usage and Examples: Sets a single prefix component in the tree display.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

RecursiveTreeIterator formats traversed entries with a prefix and suffix that reflect their place in the hierarchy.

What Is RecursiveTreeIterator::setPrefixPart in PHP?


Sets a single prefix component in the tree display.

RecursiveTreeIterator::setPrefixPart(int $part, string $value)

Parameters:

  • $part — Required. The part argument used to configure this operation; its accepted values follow the declared PHP type.
  • $value — Required. The value to store, compare or inspect.

Example 1: Understanding the Basic Behavior


<?php

$iterator = new RecursiveTreeIterator(new RecursiveArrayIterator(["group" => ["first"=>1], "status"=>"active"]));
$iterator->setPrefixPart(RecursiveTreeIterator::PREFIX_LEFT, "--");
foreach ($iterator as $entry) { echo $entry; }

Output:

--|-Array--| \-1--\-active

Explanation: Sets a single prefix component in the tree display.

Example 2: Testing Another Case


<?php

$iterator = new RecursiveTreeIterator(new RecursiveArrayIterator(["folder" => ["first"=>1], "status"=>"active"]));
$iterator->setPrefixPart(RecursiveTreeIterator::PREFIX_LEFT, "--");
foreach ($iterator as $entry) { echo $entry; }

Output:

--|-Array--| \-1--\-active

Explanation: One prefix component is changed; the source data and iterator hierarchy remain unchanged.

Practical Example: Applying the Operation


<?php

$iterator = new RecursiveTreeIterator(new RecursiveArrayIterator(["files" => ["first"=>1], "status"=>"active"]));
$iterator->setPrefixPart(RecursiveTreeIterator::PREFIX_LEFT, "--");
foreach ($iterator as $entry) { echo $entry; }

Output:

--|-Array--| \-1--\-active

Explanation: Prefix characters communicate depth; getEntry() is different from current(), which includes formatting.

Important Behavior and Edge Cases


  • One prefix component is changed; the source data and iterator hierarchy remain unchanged.
  • Prefix characters communicate depth; getEntry() is different from current(), which includes formatting.

Comparison With Related Approaches


ApproachWhen to Use It
RecursiveTreeIterator::setPrefixPartSets a single prefix component in the tree display.
ArrayIteratorRecursiveIteratorIterator traverses recursively without automatically drawing a tree.

Common Beginner Mistakes


MistakeCorrect Approach
Misinterpreting the documented behavior of RecursiveTreeIterator::setPrefixPart() for the supplied value.One prefix component is changed; the source data and iterator hierarchy remain unchanged.
Choosing ArrayIterator without checking whether it provides the same result.RecursiveIteratorIterator traverses recursively without automatically drawing a tree.

Key Takeaways


  • Sets a single prefix component in the tree display.
  • One prefix component is changed; the source data and iterator hierarchy remain unchanged.
  • Prefix characters communicate depth; getEntry() is different from current(), which includes formatting.

The examples demonstrate where RecursiveTreeIterator::setPrefixPart fits within tree-shaped output. Verify the relevant preconditions whenever you adapt the code to real data.

Share this resourceSend this page to someone who may find it useful.