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;
}
回答下面的一些问题和我想实现的。php:foreach()如何分配给两个变量
- 数组
$choices
不是数字索引。 - 数组索引为
$vtitle
。 - 它们不会短于彼此,因为我的代码在代码运行之前会处理这个问题。
- 我想返回
$options
变量。问题是$choices[0]
和$vtitle[0]
只能使用一次。希望我能够表达我的问题。 - 我不想去通过
$vtitles
阵列一次在$choices
每个值。
@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);
}
您的数组是数字索引的吗? – 2012-02-15 19:09:38
如果一个阵列比另一个阵列短,会发生什么? – Nettogrof 2012-02-15 19:10:30
定义'$ key'两次肯定会导致问题。 – 2012-02-15 19:10:55