遍历&更改多维php数组中的子键
我有一个如下所示的多维数组:
Array(
[135] => Array(
[150] => Array(
[151] => Array(
[1] => Array()
[153] => Array()
)
[1] => Array(
[1] => Array()
[2] => Array()
)
)
[1] => Array(
[1] => Array(
[1] => Array()
[2] => Array()
)
[2] => Array(
[1] => Array()
[2] => Array()
)
)
)
)
我想更改为以下内容:
Array(
[135] => Array(
[150|135] => Array(
[151|150] => Array(
[1|151] => Array()
[153|151] => Array()
)
[1|150] => Array(
[1|1] => Array()
[2|1] => Array()
)
)
[1|135] => Array(
[1|1] => Array(
[1|1] => Array()
[2|1] => Array()
)
[2|1] => Array(
[1|2] => Array()
[2|2] => Array()
)
)
)
)
我的意思是每个子键都会有 his key |父密钥
格式。树标签是固定的。深度不会比上面代码中显示的更多或更少。
最好的方法是什么?感谢您的帮助
I have an multidimensional array that looks like this:
Array(
[135] => Array(
[150] => Array(
[151] => Array(
[1] => Array()
[153] => Array()
)
[1] => Array(
[1] => Array()
[2] => Array()
)
)
[1] => Array(
[1] => Array(
[1] => Array()
[2] => Array()
)
[2] => Array(
[1] => Array()
[2] => Array()
)
)
)
)
I would like to change to the following:
Array(
[135] => Array(
[150|135] => Array(
[151|150] => Array(
[1|151] => Array()
[153|151] => Array()
)
[1|150] => Array(
[1|1] => Array()
[2|1] => Array()
)
)
[1|135] => Array(
[1|1] => Array(
[1|1] => Array()
[2|1] => Array()
)
[2|1] => Array(
[1|2] => Array()
[2|2] => Array()
)
)
)
)
I mean each child key will have his key | parent key
format. Tree label is fixed. No more or less depth than shown in the above code.
What is the best way to do this? Thank you for any assistance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
我认为这可以给你一些想法,但我还没有测试过,所以我不太确定正确性。
Try this:
I think this can give you some idea but i have not tested this so i am not very sure about the correctness.
不需要疯狂的嵌套
foreach()
循环来解决这个问题。递归是你的朋友:There's no need for crazy nested
foreach()
loops to solve this. Recursion is your friend:您完全可以按照您的要求进行操作:遍历并更改密钥(演示):
此函数使用递归进行遍历(该函数调用本身),因此它仍然可以在多个级别上运行。
更改数组键实际上是不可能的,没有像 array_rename_key 之类的功能。相反,将添加具有新密钥的项目,然后删除具有旧密钥的项目。为了不重复值,在上面的示例中使用了引用/别名:
$a
是数组,$v
是当前元素的值,>$k
密钥和$p
父级密钥。当新元素添加到数组末尾时,在 foreach 内部需要检查是否已处理某个键(这是一个新键,所有新键都是字符串)以退出。
You can exactly do like you asked: Traverse and change keys (Demo):
This function traverses with recursion (the function calls itself) so it can operate on multiple levels all the same.
Changing array keys is actually not really possible, there is not function like
array_rename_key
or such. Instead, the item with the new key is added and then the item with the old key is removed. To not duplicate values, a reference/alias is used for this in the example above:$a
is the array,$v
is the value of the current element,$k
the key and$p
the parent's key.As new elements are added at the end of the array, inside the
foreach
a check is needed to quit if a key has been already processed (it's a new key, all new keys are strings).