首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果第二个数组键等于,则从数组中回显每个键值

如果第二个数组键等于,则从数组中回显每个键值
EN

Stack Overflow用户
提问于 2015-05-12 19:42:37
回答 2查看 120关注 0票数 0

我有两个数组:

代码语言:javascript
复制
$choices = array  (
        [model] => 3D Modeling / BIM
        [edb] => Engineering & Design-Build
        [gse] => Green & Sustainable Energy
        [ipd] => Integrated Project Design
        [lc] => Lean Construction
        [mt] => Material Supply
        [ecs] => Electrical Construction & Service
        [fps] => Fire Protection Services
        [hms] => HVAC & Mechanical Service
        [tsl] => Traffic Signal & Lighting
    )
$servicesSelected = array (
        [model] => 0
        [ipd] => 1
        [lc] => 2
        [mt] => 3
    )

我试图检查任何数组2键是否等于数组1键,如果数组1键等于数组2键,则从数组1打印值。我不太确定从哪里开始。但是在这个例子中,我会重复下面的话,因为它们的关键存在于比较中。

  • 三维模块化/ BIM
  • 集成项目设计
  • 精益结构
  • 物料供应
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-12 19:56:27

这应该可以做到:

代码语言:javascript
复制
<?php
header('Content-Type:text/plain');
$choices = array  (
'model' => ' 3D Modeling / BIM',
'edb' => ' Engineering & Design-Build',
'gse' => ' Green & Sustainable Energy',
'ipd' => ' Integrated Project Design',
'lc' => ' Lean Construction',
'mt' => ' Material Supply',
'ecs' => ' Electrical Construction & Service',
'fps' => ' Fire Protection Services',
'hms' => ' HVAC & Mechanical Service',
'tsl' => ' Traffic Signal & Lighting');
$servicesSelected = array (
'model' => 0,
'ipd' => 1,
'lc' => 2,
'mt' => 3);

$printArray = array_intersect_key($choices , $servicesSelected );

var_export($printArray);
?>

结果

代码语言:javascript
复制
array (
  'model' => ' 3D Modeling / BIM',
  'ipd' => ' Integrated Project Design',
  'lc' => ' Lean Construction',
  'mt' => ' Material Supply',
)
票数 1
EN

Stack Overflow用户

发布于 2015-05-12 20:11:28

做这个

代码语言:javascript
复制
foreach($servicesSelected as $key2=>$serviceSelected)
  foreach($choices as $key1=>$choice)
    if($key1==$key2) echo $choice;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30200041

复制
相关文章

相似问题

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