EasyBB EasyBB - 7 months ago 72
PHP Question

PHP array assigning multiple keys to same value

$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

Answer

You can use array_fill_keys() :

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

Example

Comments