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