douqiao1983
douqiao1983
2018-10-06 12:28
浏览 98

我如何在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
    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.

    点赞 评论

相关推荐