3

This is killing me, for the life of me i can not seem to figure out how to get this to work, or why it doesn't work in the first place.

Here is what i'm trying to do:

here is my variables declaration:

$locale = 'US';
$realm = 'magtheridon';
$character = 'billymayys';

here is my array declaration:

$my_array = ( 'L' => $locale, 'R' => $realm, 'C' => $character );

I am new to php and from what i can tell this should create an array who would print out to:

Array( 
      [L] => US, 
      [R] => magtheridon, 
      [C] => billymayys, 
      );

But it doesn't.

What is the proper way to create an array, whos index i can name and then assign variables to the values of those indexs?

The array declaration:

$my_array = ( 'L' => 'US', 'R' => 'magtheridon', 'C' => 'billymayys' );

Works but i do not understand why i cannot dynamically assign the values using variables.

Please help! Thanks.

3
  • Are you sure they are in the same scope? You're not trying to define the array in a function with the variables from outside the function, are you? Commented Apr 11, 2013 at 19:52
  • 1
    did you try $my_array = array('L' => $locale, 'R' => $realm, 'C' => $character ); ? Commented Apr 11, 2013 at 19:52
  • what does it actually print? did u try var_dump($my_array)? Commented Apr 11, 2013 at 19:52

2 Answers 2

5

You just have a minor syntax error, missing the array keyword.

Change:

$my_array = ( 'L' => $locale, 'R' => $realm, 'C' => $character );

To:

$my_array = array( 'L' => $locale, 'R' => $realm, 'C' => $character );

Or:

$my_array = [ 'L' => $locale, 'R' => $realm, 'C' => $character ]; // PHP 5.4+

Working example: http://3v4l.org/d2UWM

Sign up to request clarification or add additional context in comments.

2 Comments

Can't believe I missed that when I commented XD
Much thanks. The absence of array was actually just a typo on my part when I created the question. BUT, it lead me to finding the real problem. I am using wordpress and my assignments were wrong, i was using $locale = the_author_meta( $current_user->id, 'locale' );, when i should have used $locale = $current_user->locale;
0

You need to use the array keyword:

$my_array = array( 'L' => $locale, 'R' => $realm, 'C' => $character );

Not sure why the second one would work!

1 Comment

The absence of the array keyword along with using: $locale = the_author_meta($current_user,'locale'); instead of: $locale=$current_user->locale; seems to have been the issue. Thanks for all the help. *im working with wordpress.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.