Walk array recursively and print the path of the walk

前端 未结 9 1508
礼貌的吻别
礼貌的吻别 2020-12-03 08:03

Can someone help me with some code or instructions on how to walk recursively an array and when reaching the last element print the full path to it? A simple echo will work

9条回答
  •  星月不相逢
    2020-12-03 08:32

     $value) {
            $string .= str_repeat("  ", $i) .' - '. $k . ':';
            if (!is_array($value)) {
                $string .= $value . '
    '; } else { $string .= '
    '; $string .= printListRecursive($value, $var, $i + 1); } } return $string; } $test_array = [ 'America' => [ 'Argentina' => 'Buenos Aires', 'Peru' => 'Lima' ], 'Europe' => [ 'Ireland' => 'Dublin', 'France' => 'Paris', 'Italy' => 'Rome' ] ]; $result = printListRecursive($test_array); echo $result; ?>

    Check code here

提交回复
热议问题