将多个键分配给数组中的相同值 [英] Assign multiple keys to same value in array

查看:82
本文介绍了将多个键分配给数组中的相同值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 $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类函数:

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

推荐答案

您可以使用 array_fill_keys() :

You can use array_fill_keys() :

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

示例

这篇关于将多个键分配给数组中的相同值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆