Question: Here is The My Array Code...

```
$data = array(
            'data_1',
            'data_2',
            'data_3',
            'data_4',
            'data_5' => array(
                            'data_5_1',
                            'data_5_2'
                        )
);

```

i Want to Ountput Like The : -

```
data_1
data_2
data_3
data_4
data_5
    data_5_1
    data_5_2

```

Here is My Code I try to Self But I Show Error

```
foreach($data as $da){ 
     echo $da."<br>";
}

```

Error Found Like This 

```
data_1
data_2
data_3
data_4
Notice: Array to string conversion in filename.php on line 3
Array

```

Please Fix this problem & Use echo not print\_r

Answer: 
This is best achieved with a recursive function so that you can deal with any level of nested arrays:

```
function display_list($array) {
    foreach ($array as $k => $v) {
        if (is_array($v)) {
            echo "$k\n";
            display_list($v);
        }
        else {
            echo "$v\n";
        }
    }
}
display_list($data);

```

Output:

```
data_1
data_2
data_3 
data_4 
data_5 
data_5_1 
data_5_2

```

[Demo on 3v4l.org](https://3v4l.org/ut9t8)
You could use iterators:

```
foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($data)) as $item)
  echo "$item<br>", PHP_EOL;

```

As asked in comments, if you want either the key or value depending on type, you can use the flag `SELF_FIRST` and the ternary operator:

```
foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($data), RecursiveIteratorIterator::SELF_FIRST) as $key => $item)
  echo (is_scalar($item) ? $item : $key) . '<br>', PHP_EOL;

```