【发布时间】:2012-03-07 03:55:46
【问题描述】:
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);
}
【问题讨论】:
-
你的数组有数字索引吗?
-
如果一个数组比另一个数组短会怎样?
-
定义
$key两次肯定会出问题。 -
PHP 不能那样工作。如果
count($arrA)== 5 和count($arrB)== 50,你会期待什么?