将多个键分配给数组中的相同值

Eas*_*yBB 9 php arrays key key-value

 $lang = array(
        'thank you'=>'You are welcome',
        'thanks'=>'You are welcome',
        'thank ya'=>'You are welcome'
    );
Run Code Online (Sandbox Code Playgroud)

正如你所看到的那样,为了相同的价值而写下多个键会让我感到厌烦.

$lang['thanks']=>$lang['thank ya']=>$lang['thank you']
Run Code Online (Sandbox Code Playgroud)

试着通过重写一百次来节省自己一些时间

PHP类功能:

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);
     }
  }
Run Code Online (Sandbox Code Playgroud)

EX

 $lang = array(
        'thank you'=>'You are welcome',
        'thanks'=>'You are welcome',
        'thank ya'=>'You are welcome',
        'hello'=>'hello',
        'goodbye'=>'goodbye'
    ); 
Run Code Online (Sandbox Code Playgroud)

所以我需要将它添加到数组中,而不是用相同的值填充我的键值,实际上它们并非完全相同.我应该在开始时说明这一点

pot*_*hin 18

你可以使用array_fill_keys():

$keys = array('thank you','thanks','thank ya');
$lang = array_fill_keys($keys, 'You are welcome');
Run Code Online (Sandbox Code Playgroud)

  • 谢谢notulysses!我很感激,我还在学习php,所以这些功能对我来说都是新的. (2认同)