Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign multiple keys to same value in array

 $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

like image 439
EasyBB Avatar asked May 17 '14 22:05

EasyBB


1 Answers

You can use array_fill_keys() :

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

Example

like image 143
potashin Avatar answered Sep 19 '22 18:09

potashin