Quick Find

Example Documents as I discover the knowledge to create them. I hope they are usefull to you.

• Mulitdimensional
  Associative Array Example

Problem: Static Text Objects (like labels for buttons and such) on a web page need translating when a user selects a different lanaguage. We don't want to create database records for template specific objects as that would just complicate the task of adding additional templates and themes.

Solution: Create a Mulitdimensional Associative Array called language that we can loop thru to select words based upon the users' requested dialect. We can then programmatically build text-snippets to place inside our html page code that will be replaced with text from the selected dialect during page rendering.

Let's think of our variable as a language that contains many words in different dialects. $language[$word][$_dialect]

So, if you wanted to select the German word for 'away' from the language array, it would look like this: $language['away']['de']
Doing an echo $language['away']['de']; would output 'entfern'. But we want a way to select from within the script. Here it is.

• Each ROW is a word in our language array.
• Each word is an Associative Array whose ROWs contains a series of dialect identifiers [keys] and word translations [values].

In the first case here, the variable language is assigned a [key] = go AND a [value] = translated word array.

We start by getting the keys and values (key=>value) from $language using a foreach loop:

foreach ($language as $word=>$translated){

For Each [key] in language, assign the contents of key to a variable called $word (use the [key] AS $word).
Then assign the [key]'s [value] to the $translated variable ($word=>$translated).

This results in a variable and an array: $word and $translated[]. These are used inside the foreach loop.
The variable $word now contains go.
While each element of the array $translated contain a dialect [key] en and translated [value] go.
We only need to echo $word to see the results.
We can return the data from $translated[] using the associative key name that the user assigned to the variable $_dialect:
$translated[$_dialect] or more literally:

echo $translated['en']; will output  go

echo $translated['de']; will output  gehst

There is no need to iterate $translated[] in a foreach loop because the [key] is identified in the variable $_dialect which was passed to the function when called.

You can run the script below By Clicking Here, change the value of $_dialect to fr, de or sp to see the different results.

function translate($_dialect,$_content){

    $language = array(

              'go'=>array('sp'=>'recorer','de'=>'gehst','fr'=>'allez tout','en'=>'go'),
              'on'=>array('sp'=>'sucesivamente','de'=>'auf','fr'=>'surce','en'=>'on'),
          'away'=>array('sp'=>'emprendio','de'=>'entfern','fr'=>'suite','en'=>'away'),
     'vacation'=>array('sp'=>'las vacaciones','de'=>'Urlaub','fr'=>'des vacances','en'=>'vacation')

    );

    foreach ($language as $word=>$translated){

       $_snippet = "{language_".$word."}";
       $_content = str_replace($_snippet,$translated[$_dialect],$_content);

    }

    $_content = "The Translated String: ".$_content;

    return $_content;

}


$dialect = 'en';
$content = "{language_go} {language_away} {language_on} {language_vacation}";

echo "Our String: ".$content."<br />";
echo translate($dialect,$content);

Did you notice the line: $_snippet = "{language_".$word."}";

This is creating a text-snippet from the current row, in our example the $word is 'go', this line inserts the $word into the text-snippet {language_) to produce {language_go}. It will do this for each and every $word in our $language array.

The next line $_content = str_replace($_snippet,$translated[$_dialect],$_content); then searches the content and replaces each instance of {language_go} with the word translated in the chosen dialect. This is done programmatically so all you need to do replace the words in your html page with language text-snippet and add the new word/translation data to the langauge array following the defined pattern.

I like to use varibles that begin with an underscore _ inside of the functions I create.

Also, if you put the $language array inside the function you won't have to declare a page full of Globals.