php: foreach() 如何分配给两个变量
function example()
{
foreach ($choices as $key => $choice) { # \__ both should run parallel
foreach ($vtitles as $keystwo => $vtitle) { # /
$options .= '<option value="'. check_plain($key) .'" title="' . $vtitle . '"' . $selected
.'>'. check_plain($choice) .'</option>';
} // end of vtitle
} // end of choice
return $options;
}
回答以下一些问题以及我想要实现的目标。
- 数组
$choices
没有数字索引。 - 数组
$vtitle
按数字索引。 - 它们不会比彼此短,因为我有代码会在该代码运行之前处理这个问题。
- 我正在尝试返回 $options 变量。问题是
$choices[0]
和$vtitle[0]
只能使用一次。希望我能够表达我的问题。 - 我不想为
$choices
中的每个值都遍历一次$vtitles
数组。
@hakre:谢谢,在你的帮助下我几乎解决了这个问题。
我收到变量 $vtitle
的错误:
InvalidArgumentException: Passed variable is not an array or object, using empty array
instead in ArrayIterator->__construct() (line 35 of /home/vishal/Dropbox/sites/chatter/sites
/all/themes/kt_vusers/template.php).
我确信它是一个数组,这是使用 print_r
的输出
Array ( [0] => vishalkh [1] => newandold )
可能出了什么问题?
以下对我有用,谢谢 hakre
while
(
(list($key1, $value1) = each($array1))
&& (list($key2, $value2) = each($array2))
)
{
printf("%s => %s, %s => %s \n", $key1, $value1, $key2, $value2);
}
function example()
{
foreach ($choices as $key => $choice) { # \__ both should run parallel
foreach ($vtitles as $keystwo => $vtitle) { # /
$options .= '<option value="'. check_plain($key) .'" title="' . $vtitle . '"' . $selected
.'>'. check_plain($choice) .'</option>';
} // end of vtitle
} // end of choice
return $options;
}
Answers to some of the below questions and what I am trying to achieve.
- Array
$choices
is not numerically indexed. - Array
$vtitle
is numerically indexed. - They won't be shorter than each other as I have code which will take care of this before this code runs.
- I am trying to return
$options
variable. The issue is that$choices[0]
and$vtitle[0]
should be used only once. Hope I was able to express my problem. - I do not want to go through the
$vtitles
array once for each value in$choices
.
@hakre: thanks I have nearly solved it with your help.
I am getting an error for variable $vtitle
:
InvalidArgumentException: Passed variable is not an array or object, using empty array
instead in ArrayIterator->__construct() (line 35 of /home/vishal/Dropbox/sites/chatter/sites
/all/themes/kt_vusers/template.php).
I am sure its an array this is the output using print_r
Array ( [0] => vishalkh [1] => newandold )
What might be going wrong ?
The below worked for me , thank you hakre
while
(
(list($key1, $value1) = each($array1))
&& (list($key2, $value2) = each($array2))
)
{
printf("%s => %s, %s => %s \n", $key1, $value1, $key2, $value2);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
它不会按照您用伪代码概述的方式工作。然而,SPL 提供了一种同时迭代多个迭代器的方法。它称为
MultipleIterator
,您可以根据需要附加任意多个迭代器:请参阅操作: 演示
编辑: 第一个示例显示了来自声压级。它的好处是它可以处理任何类型的迭代器,而不仅仅是数组。如果你想用数组表达类似的东西,你可以用经典的
while(list()=each())
循环来实现类似的东西,它允许比foreach
更多的表达式。演示(迭代最小数量的元素)
另请参阅相关问题:PHP foreach 循环中的多个索引变量
It does not work the way you outline with your pseudo code. However, the SPL offers a way to iterate multiple iterators at once. It's called
MultipleIterator
and you can attach as many iterators as you like:See it in action: Demo
Edit: The first example shows a suggestion from the SPL. It has the benefit that it can deal with any kind of iterators, not only arrays. If you want to express something similar with arrays, you can achieve something similar with the classic
while(list()=each())
loop, which allows more expressions thanforeach
.Demo (the minimum number of elements are iterated)
See as well a related question: Multiple index variables in PHP foreach loop
一般情况下,你不能在 foreach 循环中执行此操作。但你可以在 PHP5 中这样做:
在旧版本的 PHP 中,它会像这样:
you can't do it in foreach loop in general case. But you can do it like this in PHP5:
In older versions of PHP it will be like this:
简短的回答,不。
你可以做的是:
Short Answer, no.
What you can do instead is:
如果密钥相同,您可以这样做:
If the keys are the same, you can do this:
你可能必须重新调整你的逻辑才能做你想做的事。有什么理由不能只使用 2 个 foreach 循环吗?或者将其中一个嵌套在另一个中?
编辑:正如其他人指出的那样,如果 PHP 确实支持您想要做的事情,那么它根本不安全..所以它不支持可能是一件好事:)
You may have to refigure your logic to do what you want. Any reason you can't just use 2 foreach loops? Or nest one inside the other?
EDIT: as others have pointed out, if PHP did support what you're trying to do, it wouldn't be safe at all.. so it's probably a good thing that it doesn't :)
也许您想要这个:
但是如果大小相同,这就会起作用。但您可以更改此代码。
Maybe you want this:
But this will work if size is the same. But you can change this code.
您需要使用两个 foreach 循环。
You need to use two foreach loops.