11

I've been looking around for a solution to this with no real success. I have a multidimensional array of parents and children with no limits on depth. This is generated from a database but the issue is that the item ID becomes the key using my way of arranging a flat array into a multidimensional array like so:

Array(

[28] => Array
        (
            [id] => 28
            [color] => #ff24e5
            [name] => Personal
            [parent_id] => 
            [children] => Array
                (
                    [23] => Array
                        (
                            [id] => 23
                            [color] => #41c3a3
                            [name] => Shopping
                            [parent_id] => 28
                            [children] => Array
                                (
                                    [22] => Array
                                        (
                                            [id] => 22
                                            [color] => #8be32b
                                            [name] => Deals
                                            [parent_id] => 23
                                            [children] => Array
                                                (
                                                )
                                        )
                                )
                        )

                    [150] => Array
                        (
                            [id] => 150
                            [color] => #e9a3f0
                            [name] => Orders
                            [parent_id] => 28
                            [children] => Array
                                (
                                )
                        )
                )
        )
)

What I would like, is a function that does the following:

Array (
[0] => Array
        (
            [id] => 28
            [color] => #ff24e5
            [name] => Personal
            [parent_id] => 
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 23
                            [color] => #41c3a3
                            [name] => Shopping
                            [parent_id] => 28
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 22
                                            [color] => #8be32b
                                            [name] => Deals
                                            [user_id] => 1
                                            [selected] => 0
                                            [parent_id] => 23
                                            [children] => Array
                                                (
                                                )
                                        )
                                )
                        )

                    [1] => Array
                        (
                            [id] => 150
                            [color] => #e9a3f0
                            [name] => Orders
                            [parent_id] => 28
                            [children] => Array
                                (
                                )
                        )
                )
        )
)

Essentially reassign keys starting from 0. I've tried numerous methods, but I'm assuming that I need to find a recursive solution and when I tried that, it destroyed my array. I was reading up on the array_walk_recursive() function, but I don't quite know what to do beyond that. Essentially, is there a way to reset numeric keys in a multidimensional array?

Thanks for the help!

1
  • I answered your question just after you asked it. It's 2 hours later, are you going to return? If this answered your question, please click the checkmark icon next to the answer to mark it as accepted. Commented Aug 9, 2011 at 5:38

4 Answers 4

21

You really need to add the is_numeric condition to stop text keys getting mixed up...

function fix_keys($array) {

    foreach ($array as $k => $val) {

        if (is_array($val)) 
            $array[$k] = $fix_keys($val); //recurse
    }

    if( is_numeric($k) )
        return array_values($array);

    return $array;
}

I did this instead:

function fix_keys($array) {
    $numberCheck = false;
    foreach ($array as $k => $val) {
        if (is_array($val)) $array[$k] = fix_keys($val); //recurse
        if (is_numeric($k)) $numberCheck = true;
    }
    if ($numberCheck === true) {
        return array_values($array);
    } else {
        return $array;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic! This worked brilliantly for me, whereas the current correct answer did not.
16
function fix_keys($array) {
  foreach ($array as $k => $val) {
    if (is_array($val)) 
      $array[$k] = fix_keys($val); //recurse
  }
  return array_values($array);
}

2 Comments

Great! Thanks. Didn't get an email, so I only checked now.
This works but I am losing the array keys that are not numerical, how would I go about only reassigning numerical keys? Thanks
12

I was trying to fix the same problem, here is the code

$array = array_values($array);

1 Comment

This won't work for multi-dimensional arrays. The fix_keys() function above will work. 3v4l.org/vGmmJ
1

the correct answer reset all keys and dont ignore the not numeric keys, "Lobos" answer is a step in the right direction, but reset too the not numeric keys by level 2 and lower. for me this done the job perfect

function array_values_recursive($array) {
$temp = array();
foreach ($array as $key => $value) {
    if (is_numeric($key)) {
        $temp[] = is_array($value) ? array_values_recursive($value) : $value;
    } else {
        $temp[$key] = is_array($value) ? array_values_recursive($value) : $value;
    }
}
return $temp;

}

Comments

Your Answer

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.