PHP 每个循环使数组更深一层
我试图循环遍历一个数组,每次都向另一个数组添加一个新级别。让我说明一下 - 变量 $arr 的值每次都不同
$arr = array("1","5","6");
Looping
$index[$arr[0]];
Looping
$index["1"][$arr[1]] // "1" since this key was filled in by the previous loop, continuing with a new key
Looping
$index["1"]["5"][$arr[2]] // same as previous loop
--循环遍历所有 $arr 的项目,完成,结果是 $index["1"]["5"]["6"]--
问题是我不知道 $arr
数组包含多少值。然后,当 $arr
的第一个值循环到下一个数组时,我不知道如何从 $index["1"]
继续级别(换句话说:添加另一个键)..
有人吗?
I'm trying to loop through one array, adding a new level to another array each time. Let me illustrate - variable $arr's values are different each time
$arr = array("1","5","6");
Looping
$index[$arr[0]];
Looping
$index["1"][$arr[1]] // "1" since this key was filled in by the previous loop, continuing with a new key
Looping
$index["1"]["5"][$arr[2]] // same as previous loop
--looped over all $arr's items, done, result is $index["1"]["5"]["6"]--
The problem is I won't know how much values the $arr
array contains. Then, I don't know how to continue from, for example, $index["1"]
when the first value of $arr
has been looped to the next array level (other words: add another key)..
Anyone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在此处使用引用:
outputs
要使用其他值覆盖最后一个元素,您只需
在循环后面添加行:,因为 $c 是对最深数组级别的引用,当循环时完成了。
You can use references here:
outputs
To overwrite the last element with some other value, you can just add the line:
after the loop, because $c is a reference to the deepest array level, when the loop is finished.