$videoskey_list = explode(',',$result[$x]["videos_key"]);
$videosname_list = explode(',',$result[$x]["videos_name"]);
foreach($videoskey_list as $videoskey => $videos_key && $videosname_list as $videosname => $videos_name)
{
echo ' <button id="playtrailer" class="playtrailer" data-src="'.$videos_key.'"> '.$videos_name.' </button>';
}
我如何使用&amp;&amp;在foreach。它应该工作,对吗?或PHP不支持&amp;&amp;在foreach?
错误
解析错误:语法错误,意外&#39;&amp;&amp;&#39; (T_BOOLEAN_AND),
答案 0 :(得分:4)
$videos_list = array_combine($videoskey_list, $videosname_list);
foreach($videos_list as $key => $name) {
// ...
}
答案 1 :(得分:4)
如果你的键和值都意味着这应该是有用的......
$videoskey_list = explode(',',$result[$x]["videos_key"]);
$videosname_list = explode(',',$result[$x]["videos_name"]);
foreach( $videoskey_list as $index => $videos_key ) {
echo ' <button id="playtrailer" class="playtrailer" data-src="'.$videos_key.'"> '.$videosname_list[$index].' </button>';
}
EDITED:
如果我们使用array_combine
两个数组应该相等。在这里,我们可以使用我们有多少键,输出将来到这里。
在array_merge
中,两个数组都已合并,因此我们无法对相同的键和值进行精细处理。
解释对于这个答案:
首先我们获得一个videoskey_list作为键和值。 如果将Key与Value匹配。我们可以将videoskey_list的密钥用作videosname_list的索引。例如,使用此代码检查here。
$numbers = array('1','2','3');
$alpha = array('a','b','c');
foreach( $numbers as $index => $number ) {
echo $number .'->'. $alpha[$index] .'<br />';
}
答案 2 :(得分:2)
您可以使用array_combine()
$videoskey_list = explode(',',$result[$x]["videos_key"]);
$videosname_list = explode(',',$result[$x]["videos_name"]);
foreach (array_combine($videoskey_list, $videosname_list) as $videos_key => $videos_val) {
echo ' <button id="playtrailer" class="playtrailer" data-src="'.$videos_key.'"> '.$videos_val.' </button>';
}
或
使用array_merge()
foreach (array_merge($videoskey_list, $videosname_list) as $videoskey => $videos_val) {
echo ' <button id="playtrailer" class="playtrailer" data-src="'.$videos_key.'"> '.$videos_val.' </button>';
}
<强>演示强>
<?php
$videoskey_list = array('111','222','333');
$videosname_list = array('test','abc','xyz');
foreach (array_combine($videoskey_list, $videosname_list) as $videos_key => $videos_val) {
echo ' <button id="playtrailer" class="playtrailer" data-src="'.$videos_key.'"> '.$videos_val.' </button>';
}
<强>输出强>
<button id="playtrailer" class="playtrailer" data-src="111"> test </button>
<button id="playtrailer" class="playtrailer" data-src="222"> abc </button>
<button id="playtrailer" class="playtrailer" data-src="333"> xyz </button>
演示链接:Click Here