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

8
  • 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. Commented May 17, 2014 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. Commented May 17, 2014 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. Commented May 18, 2014 at 0: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 Commented May 18, 2014 at 0: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 us3.php.net/manual/en/ref.array.php Commented May 18, 2014 at 0:11

4 Answers 4

29

You can use array_fill_keys() :

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

Example

Sign up to request clarification or add additional context in comments.

9 Comments

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 : 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.
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 : Yes,exactly
@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.
|
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.

Comments

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

Comments

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";

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.