8

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)

j0k
22.8k28 gold badges81 silver badges90 bronze badges
asked Aug 13, 2012 at 23:27
3
  • 2
    why 'o why people are insisting on working with multiple-levels of nested arrays instead of using OOP ? Commented Aug 13, 2012 at 23:29
  • What did you pass into the function, the array or array[children]? Commented Aug 13, 2012 at 23:30
  • I passed the array to the function Commented Aug 13, 2012 at 23:32

5 Answers 5

4
function array_values_recursive($arr)
{
 $arr2=[];
 foreach ($arr as $key => $value)
 {
 if(is_array($value))
 { 
 $arr2[] = array_values_recursive($value);
 }else{
 $arr2[] = $value;
 }
 }
 return $arr2;
}

this function that implement array_values_recursive from array like:

array( 
 'key1'=> 'value1', 
 'key2'=> array ( 
 'key2-1'=>'value-2-1',
 'key2-2'=>'value-2-2'
 )
);

to array like:

array(
 0 => 'value1',
 1 => array (
 0 =>'value-2-1',
 1 =>'value-2-2'
 )
);
answered Sep 2, 2014 at 11:34
1
  • you should explain your answer Commented Sep 2, 2014 at 11:44
3

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;
}
answered Aug 13, 2012 at 23:35
2
  • The recursion works but all the keys in my array are changed to 0, 1, etc... (even the keys id and name). I only want the numeric keys in my array with index children to change Commented Aug 13, 2012 at 23:41
  • I simply translated your words into PHP now. Commented Aug 13, 2012 at 23:46
0

This should do it:

function array_values_recursive($arr, $key)
{
 $arr2 = ($key == 'children') ? array_values($arr) : $arr;
 foreach ($arr2 as $key => &$value)
 {
 if(is_array($value))
 {
 $value = array_values_recursive($value, $key);
 }
 }
 return $arr2;
}
answered Aug 13, 2012 at 23:42
0
0

Try this ,

 function updateData($updateAry,$result = array()){
 foreach($updateAry as $key => $values){
 if(is_array($values) && count($values) > 0){
 $result[$key] = $this->_updateData($values,$values);
 }else{
 $result[$key] = 'here you can update values';
 }
 }
 return $result;
 }
answered Apr 14, 2017 at 6:41
-2

You can used php fnc walk_array_recursive Here

answered Dec 25, 2017 at 20:24
2
  • Please explain how they'd use that to solve their problem. Commented Dec 25, 2017 at 20:46
  • I think walk_array_recursive does not walk over the tree, instead walks over leafs. So is unusable in this case. Commented Nov 24, 2018 at 3:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.