$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>';
}我如何使用&在前面。应该管用,对吧?或者PHP不支持&在前瞻?
错误
解析错误:语法错误,意外的‘&’(T_BOOLEAN_AND),
发布于 2017-06-29 05:16:36
如果你的钥匙和值都是意味着这应该是工作..。
$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>';
}编辑:如果我们使用array_combine,那么这两个数组应该相等。在这里,我们可以使用多少个键,我们有那么多的输出将得到这里。
在array_merge中,这两个数组被合并,因此我们不能细化相同的键和值。
对这一答复的解释:
首先,我们得到一个videoskey_list作为键和值。如果将键与值匹配。我们可以使用视频密钥列表的密钥作为视频名称列表的索引。例如,使用此代码检查这里。
$numbers = array('1','2','3');
$alpha = array('a','b','c');
foreach( $numbers as $index => $number ) {
echo $number .'->'. $alpha[$index] .'<br />';
}发布于 2017-06-29 05:09:18
$videos_list = array_combine($videoskey_list, $videosname_list);
foreach($videos_list as $key => $name) {
// ...
}发布于 2017-06-29 05:12:05
您可以使用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>';
}Demo
<?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>演示链接: 单击此处
https://stackoverflow.com/questions/44816740
复制相似问题