13
 $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

potashin
  • 44,205
  • 11
  • 83
  • 107
EasyBB
  • 6,176
  • 9
  • 47
  • 77
  • 5
    I would flip the array so that `'You are welcome'` points to all possible equivalents instead of all equivalents pointing to the same value. That will use less memory, too. –  May 17 '14 at 22:48
  • Hmmm see I have a very long array, and I use classes such as: see my new update. we use JS to search the string as an algorithm and it then parses out a response with the default language. So making you are welcome won't work. – EasyBB May 17 '14 at 23:57
  • I see nothing in your modification which prevents a cleaner structure. At some point you're probably doing `isset($lang[$word])` and could just as easily loop through and use the `array_search` function. –  May 18 '14 at 00:04
  • Hmmm I am a newb to php so I'm not sure on array_search or any of the array functions besides array :) I know that isset is always set because I am writing the infrastructure myself before releasing the l18n file. You'd have to show me what you are talking about since I am a newb with this all. And what my js does is if the word `hello` appears it searches js:`lang.hello` and uses that response my header for php makes the php into a js file btw – EasyBB May 18 '14 at 00:09
  • If you're new to any language, then you will always want to review the documentation as native functions are generally faster than other implementations. For PHP array functions, the documentation is at http://us3.php.net/manual/en/ref.array.php –  May 18 '14 at 00:11
  • It's also worth pointing out that in code in any language, if you have to type the same thing over and over... there's probably a better way to do it. –  May 18 '14 at 00:12
  • Yeah I've tried reading those suckers and they make me even more confused. I've always been a self learner, by using the functions on my own and learning exactly how they work on my own makes this entirely 100% easier for me. I will read on it though – EasyBB May 18 '14 at 00:13
  • Oh I know that, that is why like in JS `lang.hello = lang.thanks = lang['thank ya'] = lang['thank you'];` is easier than rewriting the value – EasyBB May 18 '14 at 00:14

4 Answers4

28

You can use array_fill_keys() :

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

Example

potashin
  • 44,205
  • 11
  • 83
  • 107
  • One question though is there are reversal of this? Because my array is more than just these thank yous etc. I need it so I can do `array_fill_keys($thanks,'you are welcome');` but is it possible to do this `'thanks'=>array_fill_keys($thanks,'you are welcome')` ? or something of that sort... – EasyBB May 17 '14 at 23:59
  • 1
    @EasyBB : Well, you can do `array_fill_keys()` for each and then merge all arrays in one using `array_merge()`.As all keys are unique then there should be no problems with it. – potashin May 18 '14 at 00:05
  • ok so do `$thanks =array('thank you','thanks','thank ya');` and `$otherArray = array('one','two','three',four');` and then merge like `array_merge($thanks,$otherArray);` ? – EasyBB May 18 '14 at 00:06
  • 1
    @EasyBB : Yes,exactly – potashin May 18 '14 at 00:09
  • THANKS :) I appreciate this a lot, care to enlighten me on what Jeremy is speaking of in my question post? – EasyBB May 18 '14 at 00:09
  • 1
    @EasyBB : He stated, that you can optimize your structure like `array('You are welcome' => array(),'hello'=>array('hello','hi',...),...)` and I think that it is a good idea too. – potashin May 18 '14 at 00:14
  • well anyways thanks for the help, I will have to trial this and think of the best way, I am trying to make a clean l18n file for my API still have a lot to learn good thing I'm going to college for this haha – EasyBB May 18 '14 at 00:25
  • @EasyBB : I am glad to help you.[I think that Jeremy Miller was talking about something like this](https://eval.in/152709). However, it is 4:30 on the clock and can't really focus on it, I need to get some sleep) – potashin May 18 '14 at 00:28
  • I posted something for you to look at as an answer to this question. –  May 18 '14 at 00:55
3

While I am reticent to offer up a code solution when you've admitted you are new to the language and just haven't researched it well, I'm going to hope that this project is you playing with the language to learn it as opposed to jumping in head first to give something to a client where it will ultimately not perform well.

Edit: Just saw your "good thing I'm going to college for this" and am I glad I posted to help.

Here's a structure which does what I believe you are seeking to do.

<?php
class StandardizeSayings {
  public static $CONVERSIONS = array(
    'You are welcome' => array(
      'thank you',
      'thanks',
      'thank ya'
      ),
    'Hello' => array('hello'),
    'Goodbye' => array('goodbye', 'good bye')
  );

  public static function getStandardization($word) {
    $word_lowercase = strtolower($word);
    foreach (StandardizeSayings::$CONVERSIONS as $conversion=>$equivalents) {
      if (array_search($word_lowercase, $equivalents) !== false) {
        return $conversion;
      }
    }
    return '';
  }
}

echo StandardizeSayings::getStandardization('thank ya');
?>

It uses a class structure with static members/methods (so no instantiation of the class is needed). It is easy to extend with a pre-defined list of conversions (work is needed to add in additional conversions at runtime.) It should also run fairly fast.

2

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

Mike Casan Ballester
  • 1,690
  • 19
  • 33
2

I know this question is old but I think my answer might help someone:

$arr = [
    "one" => "value",
    "two" => "more value"
];

$arr["three"] = $arr["four"] = $arr["five"] = "same value";
Muhammad Bilal
  • 497
  • 1
  • 5
  • 12