array_values recursive php
arrays, multidimensional-array, php, recursion
Solution
You use a recursive approach but you do not assign the return value of the function call `$this->array_values_recursive($value);` anywhere. The first level works, as you modify `$arr` in the loop. Any further level does not work anymore for mentioned reasons.
If you want to keep your function, change it as follows (untested):
function array_values_recursive($arr)
{
foreach ($arr as $key => $value)
{
if (is_array($value))
{
$arr[$key] = $this->array_values_recursive($value);
}
}
if (isset($arr['children']))
{
$arr['children'] = array_values($arr['children']);
}
return $arr;
}
Problem
Let's say I have an array like this: ``` Array ( [id] => 45 [name] => john [children] => Array ( [45] => Array ( [id] => 45 [name] => steph [children] => Array ( [56] => Array ( [id] => 56 [name] => maria [children] => Array ( [60] => Array ( [id] => 60 [name] => thomas ) [61] => Array ( [id] => 61 [name] => michelle ) ) ) [57] => Array ( [id] => 57 [name] => luis ) ) ) ) ) ``` What I'm trying to do is to reset the keys of the array with keys `children` to 0, 1, 2, 3, and so on, instead of 45, 56, or 57. I tried something like: ``` function array_values_recursive($arr) { foreach ($arr as $key => $value) { if(is_array($value)) { $arr[$key] = array_values($value); $this->array_values_recursive($value); } } return $arr; } ``` But that reset only the key of the first children array (the one with key 45)