首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在PHP中使用& in foreach?

如何在PHP中使用& in foreach?
EN

Stack Overflow用户
提问于 2017-06-29 05:06:17
回答 3查看 3.5K关注 0票数 2
代码语言:javascript
复制
   $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),

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-06-29 05:16:36

如果你的钥匙和值都是意味着这应该是工作..。

代码语言:javascript
复制
$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作为键和值。如果将键与值匹配。我们可以使用视频密钥列表的密钥作为视频名称列表的索引。例如,使用此代码检查这里

代码语言:javascript
复制
$numbers = array('1','2','3');
$alpha = array('a','b','c');
foreach( $numbers as $index => $number ) {
  echo $number .'->'. $alpha[$index] .'<br />';
}
票数 5
EN

Stack Overflow用户

发布于 2017-06-29 05:09:18

代码语言:javascript
复制
$videos_list = array_combine($videoskey_list, $videosname_list);
foreach($videos_list as $key => $name) {
    // ...
}
票数 4
EN

Stack Overflow用户

发布于 2017-06-29 05:12:05

您可以使用array_combine()

代码语言:javascript
复制
$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()

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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>';
}

输出

代码语言:javascript
复制
<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>

演示链接: 单击此处

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44816740

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档