Voting

: one plus three?
(Example: nine)

The Note You're Voting On

aidan at php dot net
15 years ago
This example demonstrates using the getDepth() method with a RecursiveArrayIterator.

<?php
$tree
= array();
$tree[1][2][3] = 'lemon';
$tree[1][4] = 'melon';
$tree[2][3] = 'orange';
$tree[2][5] = 'grape';
$tree[3] = 'pineapple';

print_r($tree);

$arrayiter = new RecursiveArrayIterator($tree);
$iteriter = new RecursiveIteratorIterator($arrayiter);

foreach (
$iteriter as $key => $value) {
$d = $iteriter->getDepth();
echo
"depth=$d k=$key v=$value\n";
}
?>

The output of this would be:

Array
(
[1] => Array
(
[2] => Array
(
[3] => lemon
)

[4] => melon
)

[2] => Array
(
[3] => orange
[5] => grape
)

[3] => pineapple
)

depth=2 k=3 v=lemon
depth=1 k=4 v=melon
depth=1 k=3 v=orange
depth=1 k=5 v=grape
depth=0 k=3 v=pineapple

<< Back to user notes page

To Top