Assign multiple keys to same value in array

不羁岁月 提交于 2019-12-20 10:03:03

问题


 $lang = array(
        'thank you'=>'You are welcome',
        'thanks'=>'You are welcome',
        'thank ya'=>'You are welcome'
    );

As you can see this is going to get tiresome writing multiple keys for the same value is there any way I can do.

$lang['thanks']=>$lang['thank ya']=>$lang['thank you']

Just trying to save myself some time here from rewriting a hundred times

PHP class function:

function fetch_key($key, $l,$bool){
    $dynamic = new l18n;
     if($bool == true or is_null($bool)){
        return addslashes( $dynamic->convert($key,$l) );
     }else{
      return  $dynamic->convert($key,$l);
     }
  }

EX

 $lang = array(
        'thank you'=>'You are welcome',
        'thanks'=>'You are welcome',
        'thank ya'=>'You are welcome',
        'hello'=>'hello',
        'goodbye'=>'goodbye'
    ); 

So I'd need to make it so it adds it to the array and not fill my key values with the same value when in fact they aren't all the exact same. I should have stated this in the beginning


回答1:


You can use array_fill_keys() :

$keys = array('thank you','thanks','thank ya');
$lang = array_fill_keys($keys, 'You are welcome');

Example




回答2:


While I am reticent to offer up a code solution when you've admitted you are new to the language and just haven't researched it well, I'm going to hope that this project is you playing with the language to learn it as opposed to jumping in head first to give something to a client where it will ultimately not perform well.

Edit: Just saw your "good thing I'm going to college for this" and am I glad I posted to help.

Here's a structure which does what I believe you are seeking to do.

<?php
class StandardizeSayings {
  public static $CONVERSIONS = array(
    'You are welcome' => array(
      'thank you',
      'thanks',
      'thank ya'
      ),
    'Hello' => array('hello'),
    'Goodbye' => array('goodbye', 'good bye')
  );

  public static function getStandardization($word) {
    $word_lowercase = strtolower($word);
    foreach (StandardizeSayings::$CONVERSIONS as $conversion=>$equivalents) {
      if (array_search($word_lowercase, $equivalents) !== false) {
        return $conversion;
      }
    }
    return '';
  }
}

echo StandardizeSayings::getStandardization('thank ya');
?>

It uses a class structure with static members/methods (so no instantiation of the class is needed). It is easy to extend with a pre-defined list of conversions (work is needed to add in additional conversions at runtime.) It should also run fairly fast.




回答3:


I do it in three steps:

1 - Define unique values

2 - Fill repetitive value

3 - Union 1. and 2.

$lang = array(
    'hello'=>'hello', 
    'goodbye'=>'goodbye'
);

$keys = array('thank you','thanks','thank ya');
$result = array_fill_keys($keys, 'You are welcome');

$lang += $result;

Have a look at array_fill_keys and Array Operators +=



来源:https://stackoverflow.com/questions/23716804/assign-multiple-keys-to-same-value-in-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!