PHP array question

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

LVL 4
Brant SnowAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

saimazzCommented:
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
Ray PaseurCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
clampsCommented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

zappafan2k2Commented:
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
kelvinwkwCommented:
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
LSTTCommented:
I agree on keeping language files in separate locations as kelvinwkw suggested.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.