Reset array keys in multidimensional array

php reset array keys
php array without keys
php remove keys from array
how to get data from array of array in php
php remove key from multidimensional array
php reset array keys to numeric
convert associative array to array php
php get all values from multidimensional array

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!

function fix_keys($array) {
  foreach ($array as $k => $val) {
    if (is_array($val)) 
      $array[$k] = fix_keys($val); //recurse
  }
  return array_values($array);
}

Reset keys of array elements in php?, To reset the keys of all arrays in an array: $arr = array_map('array_values', $arr); In case you just want to reset first-level array keys, use array_values() without array_map . The array_values () function takes input an array and return the indexed array of only values from the input array. Now, to reset the keys of array elements two operations can be performed, key_swap () and key_change (). As these are not inbuilt function these has to implemented in the code.

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;
    }
}

reset - Manual, PHP 5, PHP 7). reset — Set the internal pointer of an array to its first element I prefer this solution as you don't have to create the keys array. This should (not  Method 4 (the use of array_multisort() in combination with array_map()) is the second fastest in fact almost twice as fast as the usort() method, while using the same amount of memory. Method 1 (usort() + callback function) is slow when it comes to dealing with large amounts of data on this scenario.

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

$array = array_values($array);

Fix array keys for multidimensional array • PHP Developers Network, What I'm trying to do is get the array keys for each array object (other is an array of things), you foreach over that and reset keys as you go. There's a lot of multidimensional array_keys function out there, but each of them only merges all the keys in one flat array. Here's a way to find all the keys from a multidimensional array while keeping the array structure.

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;

}

Reset keys of array elements using PHP ?, PHP | Imagick getGravity() Function · Multidimensional Associative Array in PHP Now, to reset the keys of array elements two operations can be performed,  array_intersect — Computes the intersection of arrays; array_key_exists — Checks if the given key or index exists in the array; array_key_first — Gets the first key of an array; array_key_last — Gets the last key of an array; array_keys — Return all the keys or a subset of the keys of an array

PHP - How to reindex array key after unset key?, we will reassign keys from numeric like 0 1 2 etc. Sometime you need to store json to database column at that time it is better if you keep array key  reset — Set the internal pointer of an array to its first element. reset ( array ) : mixed. reset() rewinds array's internal pointer to the first element and returns the value of the first array element.

PHP Multidimensional Arrays, A multidimensional array is an array containing one or more arrays. PHP supports multidimensional arrays that are two, three, four, five, or more levels deep. Multidimensional arrays in Python provides the facility to store different type of data into a single array ( i.e. in case of multidimensional list ) with each element inner array capable of storing independent data from the rest of the array with its own length also known as jagged array, which cannot be achieved in Java, C, and other languages.

An overview of every main PHP array function (SOLVED), (Doesn't look in nested keys in multidimensional arrays.) Note that array_slice() will reorder and reset the integer array indices by default. You can change this  So here you got 3 ways to change array key without changing the order of array in PHP. And the last one works with a multidimensional array as well. All you need is just to provide correct set of “old key, new key” pairs for changing purpose.

Comments
  • 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.
  • 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
  • Fantastic! This worked brilliantly for me, whereas the current correct answer did not.
  • This won't work for multi-dimensional arrays. The fix_keys() function above will work. 3v4l.org/vGmmJ