foreach 循环内的自制函数?
我有一个基于七层多维数组的大型函数?
当我像这样在 foreach 中调用函数时,
foreach($items as $item) {
makeItem($item, $items[$item]);
}
它总是失败,为什么会这样,尽管如果我在 foreach 之外调用它们,它就可以了并且可以工作?
I have a large function that is based on a seven tier multidimensional array?
When I call the function in a for each like so
foreach($items as $item) {
makeItem($item, $items[$item]);
}
It always fails why is this, although if I call them outside a foreach it is fine and works?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,现在您已经稍微更改了代码...问题是,当您像以前一样使用 foreach 循环时,循环变量不是数组的索引,而是数组中的元素。我仍然不清楚你的意图,但你可能想做类似的事情:
...这仍然没有意义,因为你会传入
$item
两次。OK, now that you've changed the code a bit... The problem is that when you use a foreach loop as you did, the loop variable isn't the index of the array, it's the element in the array. Your intent is still unclear to me, but you might want to do something like:
...which still wouldn't make sense because you would be passing in
$item
twice.很难从问题中看出,但如果您询问如何从
foreach
循环内部调用自己的函数并将一个项目传递给该函数,我相信您正在寻找的代码是:< a href="http://php.net/manual/en/control-structs.foreach.php" rel="nofollow">http://php.net/manual/en/control-structs.foreach.php< /a>
Hard to tell from the question, but if you are asking how to call your own function from inside a
foreach
loop and pass an item to that function, I believe that the code you are looking for is:http://php.net/manual/en/control-structures.foreach.php
为什么你在 foreach 中写了三次这个函数。每次只写一次就可以调用它。或者你能描述一下或发布一些更多的代码片段吗?
Why you have written the function three times in foreach.it can be called each time by writting it only once.or could you please describe or post some more code snipped .