如果第二个数组密钥等于数组,则回声每个键值



我有两个数组:

$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键,如果该密钥等于数组2键,则从数组1打印值。我不确定从哪里开始。但是在示例中,我会回应以下内容,因为它们的keey存在于比较中。

  • 3D Modleling/bim
  • 集成项目设计
  • 精益贡献
  • 材料供应

这应该做到:

<?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);
?>

结果

array (
  'model' => ' 3D Modeling / BIM',
  'ipd' => ' Integrated Project Design',
  'lc' => ' Lean Construction',
  'mt' => ' Material Supply',
)

做这个

foreach($servicesSelected as $key2=>$serviceSelected)
  foreach($choices as $key1=>$choice)
    if($key1==$key2) echo $choice;

相关内容

  • 没有找到相关文章

最新更新