Assign multiple keys to same value in array

后端 未结 3 1750
轮回少年
轮回少年 2021-02-03 09:59
 $lang = array(
        \'thank you\'=>\'You are welcome\',
        \'thanks\'=>\'You are welcome\',
        \'thank ya\'=>\'You are welcome\'
    );

3条回答
  •  一整个雨季
    2021-02-03 10:33

    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 +=

提交回复
热议问题