在多维数组复位数组键 [英] Reset array keys in multidimensional array

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

问题描述

我一直在四处寻找一个解决的办法,没有真正的成功。我有父母和子女的多维数组与深度没有限制。这是一个由数据库生成,但问题是,项目ID使用安排一个平面数组多维数组像这样我的方式成为关键:

 阵列([28] =>排列
        (
            [ID] => 28
            [颜色] => #ff24e5
            [名] =>个人
            [PARENT_ID] =>
            [儿童] =>排列
                (
                    [23] =>排列
                        (
                            [ID] => 23
                            [颜色] => #41c3a3
                            [名] =>购物
                            [PARENT_ID] => 28
                            [儿童] =>排列
                                (
                                    [22] =>排列
                                        (
                                            [ID] => 22
                                            [颜色] => #8be32b
                                            [名] =>交易
                                            [PARENT_ID] => 23
                                            [儿童] =>排列
                                                (
                                                )
                                        )
                                )
                        )                    [150] =>排列
                        (
                            [ID] => 150
                            [颜色] => #e9a3f0
                            [名] =>命令
                            [PARENT_ID] => 28
                            [儿童] =>排列
                                (
                                )
                        )
                )
        )

我想什么,是做以下功能:

 阵列(
[0] =>排列
        (
            [ID] => 28
            [颜色] => #ff24e5
            [名] =>个人
            [PARENT_ID] =>
            [儿童] =>排列
                (
                    [0] =>排列
                        (
                            [ID] => 23
                            [颜色] => #41c3a3
                            [名] =>购物
                            [PARENT_ID] => 28
                            [儿童] =>排列
                                (
                                    [0] =>排列
                                        (
                                            [ID] => 22
                                            [颜色] => #8be32b
                                            [名] =>交易
                                            [USER_ID] => 1
                                            [选择] => 0
                                            [PARENT_ID] => 23
                                            [儿童] =>排列
                                                (
                                                )
                                        )
                                )
                        )                    [1] =>排列
                        (
                            [ID] => 150
                            [颜色] => #e9a3f0
                            [名] =>命令
                            [PARENT_ID] => 28
                            [儿童] =>排列
                                (
                                )
                        )
                )
        )

从本质上重新分配从0我试过很多方法,开始键,但我假设我需要找到一个递归解决方案,当我试过了,它毁了我的数组。我读了关于array_walk_recursive()函数,但我不太知道该怎么办不止于此。从本质上讲,是有办法在多维阵列重设数字键?

感谢您的帮助!


解决方案

 函数fix_keys($数组){
  的foreach($数组作为$ K => $ VAL){
    如果(is_array($ VAL))
      $阵列[$ K] = fix_keys($ VAL); //递归
  }
  返回array_values​​($数组);
}

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

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

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