douqiao1983 2018-10-06 12:28
浏览 101
已采纳

我如何在php中获得密钥的特定价值

I'm getting all value of this array. I want to get a whom key value in a PHP variable how can i do that.

$fields = array('firstNameInput' => 'Name', 'lastNameInput' => 'Surname', 'countryCodeInput' => 'Country', 'phoneNumberInput' => 'Phone', 'emailInput' => 'Email', 
'companyNameInput' => 'Company', 'whom' => 'Connect to', 'subjectInput' => 'Subject',  'messageInput' => 'message'
);

foreach ($_POST as $key => $value) {
            if (isset($fields[$key])) {
                $emailText .= "$fields[$key]: $value
".$another;
            }
        }
  • 写回答

1条回答 默认 最新

  • duanjiongzhen2523 2018-10-06 18:53
    关注

    I making a code for you, this code is helpful for you.

    $fields = array(
                'firstNameInput' => 'Name', 
                'lastNameInputd' => 'Surname', 
                'countryCodeInput' => 'Country', 
                'phoneNumberInput' => 'Phone', 
                'emailInput' => 'Email',
                'companyNameInput' => 'Company', 
                'whom' => 'Connect to', 
                'subjectInput' => 'Subject',
                'messageInput' => 'message'
                );
     $fields2 = array('whom');
     $fields2=array_flip($fields2);
     $result=array_intersect_key($fields,$fields2);
    
      foreach($result as $value)
      {
         echo $value.'<br>';
      }
    

    I make a new array and pass keys which you want(any key you can pass) in $fields2. now array is Array ( [0] => whom ) array_filp($fields2) is using for convert value to key . array_intersect_key($fields,$fields2) given result is

    Array ( [whom] => Connect to )
    

    finally you have a key result.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画