重置多维数组中的数组键 [英] Reset array keys in multidimensional array

查看:61
本文介绍了重置多维数组中的数组键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找解决方案,但没有真正成功.我有一个多维数组的父母和孩子,没有深度限制.这是从数据库生成的,但问题是使用我将平面数组排列成多维数组的方式,项目 ID 成为关键,如下所示:

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
                                (
                                )
                        )
                )
        )
)

基本上从 0 开始重新分配键.我尝试了很多方法,但我假设我需要找到一个递归解决方案,当我尝试这样做时,它破坏了我的数组.我正在阅读 array_walk_recursive() 函数,但我不太知道除此之外还能做什么.本质上,有没有办法重置多维数组中的数字键?

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?

感谢您的帮助!

推荐答案

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

这篇关于重置多维数组中的数组键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆