public static function depth($data, array $options = array())
{
$defaults = array('all' => false, 'count' => 0);
$options += $defaults;
if (!$data) {
return 0;
}
if (!$options['all']) {
return is_array(reset($data)) ? static::depth(reset($data)) + 1 : 1;
}
$depth = array($options['count']);
if (is_array($data) && reset($data) !== false) {
foreach ($data as $value) {
$depth[] = static::depth($value, array('all' => $options['all'], 'count' => $options['count'] + 1));
}
}
return max($depth);
}
public function testDepthFiveLevelsWithAll() { $data = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1')))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))); $result = Set::depth($data, array('all' => true)); $this->assertEqual(5, $result); $data = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))); $result = Set::depth($data, array('all' => true)); $this->assertEqual(5, $result); }