0

This issue is driving me crazy, could someone please tell whats going wrong with this!? (NOTE: Chances are that it is something really stupid but I can't seem to figure out.)

On a CodeIgniter framework I have a language key like this:

$lang['android'] = array
(
    'title' => 'Android apps',
    'image_id' => 5,
    'description' => 'Learn how to create your own Android app',
    'level' => 'Beginner'
);

Then on a view file I load this key like this:

<?php
    echo lang($course)['title'];
?>

This works perfectly on localhost, and if $course is 'android' it will print "Android apps". However this very same code does not work on the real server! It simply fails and shows me the error "PHP Parse error: syntax error, unexpected '[', expecting ',' or ';' in [filename]"

Why does this happens? Is it probably a lower PHP version? If so how I could access this array key??

Additional info: Doing a

var_dump('android');

Throws:

array(4) { ["title"]=> string(12) "Android apps" ["image_id"]=> int(5) ["description"]=> string(40) "Learn how to create your own Android app" ["level"]=> string(8) "Beginner" }

Which is the expected value... So why does this happens?

adelriosantiago
  • 7,762
  • 7
  • 38
  • 71
  • Which line you got the error? Is it one of those you've shown? – Goikiu Jan 08 '14 at 09:02
  • $course probably doesn't hold the value you think it holds. Try var_dump ($course); With issues like these, logging and dumping are usually your most effective debugging tools. When in doubt, dump it out. – GordonM Jan 08 '14 at 09:08
  • 1
    Your live server probably has a PHP version lower than 5.4 (as the link @AmalMurali provided tells you). – putvande Jan 08 '14 at 09:12
  • Thanks everyone for the help! It helped me to get on the right track, I already solved it and created and shared a function that can be pasted on CodeIgniter to access keys from arrays. – adelriosantiago Jan 08 '14 at 09:31

2 Answers2

3

Unbeliavable, yes I'm running a PHP server lower than 5.4... So dereferencing does not work.

I already solved it, you can paste the following function on your CodeIgniter:

function lang_arr($line, $key = '')
    {
        $CI =& get_instance();
        $line = $CI->lang->line($line);
        if ($key != '')
        {
            $line = $line[$key];
        }

        return $line;       
    }

Paste it on the file "system/helpers/language_helpers" just next to the function "lang" which does not supports arrays.

And now you should be able to access language array keys like this:

$lang['word'] = array
(
    'key1' => 'value1',
    'key2' => 'value2'
);

On the code simply call:

echo lang_arr($word, 'key');

For example

echo lang_arr('word', 'key1'); //Will echo 'value1'

Thanks to all commenters it helped me to get on the right track.

adelriosantiago
  • 7,762
  • 7
  • 38
  • 71
0
<?php
$lang['android'] = array
(
    'title' => 'Android apps',
    'image_id' => 5,
    'description' => 'Learn how to create your own Android app',
    'level' => 'Beginner'
);
$course ='android'; //here you have to assign
echo($lang[$course]['title']);
?>
user1844933
  • 3,296
  • 2
  • 25
  • 42
  • This throws an error: "Severity: Notice, Message: Undefined variable: lang". Anyways it seems to be needed to be very close to the answer since it makes logic. The variable $lang is, for some reason not visible as it is defined in another file. I would look for an anwser that keeps a separated structure. – adelriosantiago Jan 08 '14 at 09:22