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
| Approach | When to Use It |
|---|---|
RecursiveTreeIterator::setPrefixPart | Sets a single prefix component in the tree display. |
ArrayIterator | RecursiveIteratorIterator traverses recursively without automatically drawing a tree. |
Common Beginner Mistakes
| Mistake | Correct 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.