function traverse_array($array, $depth) { $prefix = !$depth ? '' : str_repeat(" ", $depth); $counter = 0; foreach ($array as $key => $value) { echo $prefix . ++$counter . ". " . $key . "\n"; if (is_array($value) && count($value)) { traverse_array($value, $depth + 1); } } }
/** * @covers ::traverse_array * @dataProvider provideNonLists * @expectedException InvalidArgumentException */ public function test_throws_InvalidArgumentException_when_list_is_not_an_array_or_Traversable($list) { // ---------------------------------------------------------------- // setup your test // this will hold the list built by our callable $actualList = []; // the test uses a valid callable just to make sure this // is not triggering any exceptions of any kind $callable = function ($value, $key, $name) use(&$actualList) { $actualList[$name] = $value; }; // ---------------------------------------------------------------- // perform the change traverse_array($list, '$list', $callable); // ---------------------------------------------------------------- // test the results }