Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
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? – Kolink Apr 11 at 19:52
1  
did you try $my_array = array('L' => $locale, 'R' => $realm, 'C' => $character ); ? – Fabien Apr 11 at 19:52
what does it actually print? did u try var_dump($my_array)? – sailingthoms Apr 11 at 19:52

2 Answers

up vote 4 down vote accepted

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

share|improve this answer
Can't believe I missed that when I commented XD – Kolink Apr 11 at 19:54
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; – user2165116 Apr 11 at 20:05

You need to use the array keyword:

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

Not sure why the second one would work!

share|improve this answer
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. – user2165116 Apr 11 at 20:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.