?
Solved

PHP array question

Posted on 2011-10-12
6
Medium Priority
?
202 Views
Last Modified: 2012-05-12
You can see below what i am trying to do.  Instead of typing

$text_es["hello"]

I want to write it like

$text_[$lang]["hello"];

$lang = 'es'

so i want $text_[$lang] to basically equal $text_es but i cant get it to work.
$lang = 'es';

$text_es = array("hello"=>"el mucho hello");

echo($text_[$lang]['hello']);

Open in new window

0
Comment
Question by:Brant Snow
6 Comments
 
LVL 3

Expert Comment

by:saimazz
ID: 36959135
i suggest you tu use multi array for several labguages

<?php
$lang = 'es';

$text[$lang] = array("hello"=>"el mucho hello");

echo $text[$lang]['hello'];
    ?>

Open in new window

0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 36959221
Follow the variables through this code and you will see how it works.

http://www.laprbass.com/RAY_temp_thawts.php

Outputs:
hello in en = hello
hello in fr = bonjour
goodbye in it = arrivederci

please post back if you have any questions, ~Ray
<?php // RAY_temp_thawts.php
error_reporting(E_ALL);


// DEMONSTRATE HOW TO USE TWO DIMENSIONAL ARRAYS


function translate_english($myword, $target="en")
{
    $words = array
    ( 'en' => array( 'hello'   => 'hello'   , 'goodbye' => 'goodbye' )
    , 'fr' => array( 'hello'   => 'bonjour' , 'goodbye' => 'au revoir' )
    , 'it' => array( 'hello'   => 'ciao'    , 'goodbye' => 'arrivederci' )
    )
    ;

    if ( isset($words[$target][$myword]))
    {
        return $words[$target][$myword];
    }
    else
    {
        return "I dunno";
    }
}

// TEST CASES DEMONSTRATE THE FUNCTION
$word = 'hello';
$lang = 'en';
$resp = translate_english($word, $lang);
echo "<br/>$word in $lang = $resp";

$word = 'hello';
$lang = 'fr';
$resp = translate_english($word, $lang);
echo "<br/>$word in $lang = $resp";

$word = 'goodbye';
$lang = 'it';
$resp = translate_english($word, $lang);
echo "<br/>$word in $lang = $resp";

Open in new window

0
 
LVL 3

Expert Comment

by:clamps
ID: 36960370
Hi,

I suggest multi-dimensional arrays as well since they're easy to use and very comfortable.
But it can be done the way you want with a litle workaround...

Solution with workaround:
$lang = 'es';
$text_es = array("hello"=>"el mucho hello");
$lang_temp = 'text_'.$lang;
$text_temp = $$lang_temp;
echo($text_temp['hello']);

an explanation for this:
$lang_temp will be a string 'text_es'
with $$lang_temp you can access the values in $text_es since
unfortunately you can't access an array like that, that's why you have to put the contents of $$lang_temp (which is the same as $text_es) in a new variable for example $text_temp and now you can echo the contents. If you change $lang, the variable variablename will change and therefore $lang_temp will point to another array for example text_fr.

The better solution are multi-dimensional arrays. For your example that would be:

$lang = 'es';
$text['es'] = array("hello"=>"el mucho hello");
echo ($text[$lang]['hello']);

another way to do this is:

$lang = 'es';
$text['es']['hello'] = "el mucho hello";
echo ($text[$lang]['hello']);

Cheers
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:zappafan2k2
ID: 36963320
I agree with the others' suggestions about using a multi-dimensional array.  It is a better design.

But if you just want the solution that you specified, it could be done thusly:
$lang = 'es';

$text_es = array("hello"=>"el mucho hello");

echo ${'text_'.$lang}['hello'];

Open in new window

0
 
LVL 11

Expert Comment

by:kelvinwkw
ID: 37022487
Or you could have the language file stored in different PHP file

For example
en.php
$text['hello'] = "hello";

fr.php
$text['hello'] = "bonjour";

perform an include on the language file, then you can
if ( $lang == 'en' )
  include("en.php");

echo $text["hello"];

this will gradually reduce the complexity of the php file
0
 

Expert Comment

by:LSTT
ID: 37023334
I agree on keeping language files in separate locations as kelvinwkw suggested.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses
Course of the Month16 days, 11 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question