1

Here is what I have so far:

$arrayPrices = array(
    translate($lang_type, "A/C System Evaluation") => "19.95",
    translate($lang_type, "A/C Evaluation & Recharge") => "99.00"
);

And my translate function is:

function translate($to_lan, $text) {
if($to_lan == "en") {

    return $text;

} else {

    $translate_feed = @file_get_contents('http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=' . BING_APPID . '&text=' . urlencode($text) . '&from=en&to=' . $to_lan . '');
    $translate = simplexml_load_string($translate_feed);

    return ($translate_feed === false) ? $text : $translate[0];
   }
 }

For some reason, I can't display that translate function inside of my PHP Array.

If I type in echo translate($lang_type, "A/C System Evaluation"); it works just fine and translates. But when used in that array it just returns blank.

Does anyone have any idea what I can do?

1
  • 1
    Syntactically correct, so might be some side-effects like uninitialized state, or the second request being blocked. You certainly shouldn't initialize the array from remote API calls. Especially if some of those values might never be used. Commented Apr 12, 2012 at 23:33

3 Answers 3

2

From the PHP Array docs:

The key can either be an integer or a string. The value can be of any type.

Put your keys in string vars first,like:

$var1 = translate($lang_type, "A/C System Evaluation");
$var2 = translate($lang_type, "A/C Evaluation & Recharge");

$arrayPrices = array(
    "$var1" => 19.95
    "$var2" => 29.95
);

That should work fine.

4
  • You can simply cast the result of calling translate to a string directly with (string) translate(...) => ... or strval. Commented Apr 12, 2012 at 23:33
  • @DavidHarkness There are many ways to go about it, this is the simplest IMO which I thought the op would appreciate. Why not post an answer of your own? Commented Apr 12, 2012 at 23:36
  • Your answer addressed the problem--that the values returned by transate are not strings. I am only pointing out simpler methods of creating a string from those values. You should still get the credit. :) Commented Apr 12, 2012 at 23:40
  • Perfect. Thanks so much. I tried before to put it in a variable but I didn't put the quotes around it which is why it wasn't working! Commented Apr 12, 2012 at 23:48
1

does this work:

$arrayPrices[translate($lang_type, "A/C System Evaluation")]= "19.95";
$arrayPrices[translate($lang_type, "A/C Evaluation & Recharge")] = "99.00";
2
  • I think you've got an extra set of brackets in there - that's going to build a 2-D array. Commented Apr 12, 2012 at 23:31
  • Have you tried this? It doesn't work for me when I return a class that implements __toString. Commented Apr 12, 2012 at 23:43
0

I presume you want to be able to add extensively to that product list without having to mess around with temporary variables a great deal. This is one of those situations where I'd do a post-processing run on the array, like so:

$arrayPrices = array(
    "A/C System Evaluation" => "19.95",
    "A/C Evaluation & Recharge" => "99.00",
    // ... etcetera ...
);

$keys = array_keys( $arrayPrices );    
foreach( $keys as $keyText )
{
    $translatedKeyText = translate($lang_type, $keyText);
    if ( $translatedKey != $keyText )
    {
        $arrayPrices[$translatedKeyText] = $arrayPrices[$keyText];
        unset( $arrayPrices[$keyText] );
    }
}

If you use temporary variables, you'll have to add logic for every new entry to your original array. That sounds like a maintenance hassle to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.