I am trying to convert a multi-directional to an array of keys with delimiter with a recursive php function. Here is what i have so far. Not sure why its returning partial array.
$array = [
'arr' => 1,
'arr2' => [
'_arr2' => 'test',
'_arr2_2' => 'test 2',
'_arr2_3' => 3,
'_arr2_4' => [
'__arr2' => 'tt',
'__arr2_2' => 'ttww',
'sub-sub-field' => [
'ww' => 1
]
],
'_arr2_5' => [
'__arr2_5' => 'some'
]
],
'arr3' => 'test',
'arr4' => null,
'arr5' => [
'_arr5' => [
'___arr5' => 1
],
'_arr5_1' => null
],
];
function print_array_reccur ($array, $str = '')
{
if (empty($array)) return null;
$arr = [];
foreach ($array as $key => $val)
{
if (is_array($val))
{
if ($str == '')
{
$arr = print_array_reccur($val, $key);
}
else
{
$arr = print_array_reccur($val, $str . '/' . $key);
}
}
else
{
if ($str == '')
{
$arr[$key] = $val;
}
else
{
$arr[$str . '/' . $key] = $val;
}
}
}
return $arr;
}
currently i am getting the below partial array output. Any idea as to why this is happening?
Array
(
[arr5/_arr5/___arr5] => 1
[arr5/_arr5_1] =>
)