Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This seem so simple, and yet I can't find a solution anywhere.

What I want to do is add the contents (r-value) of a variable to an associative array instead of a reference to the variable.

For example, I want this:

$myStr1 = "sometext";
$myStr2 = "someothertext";

$myArray = array(
    "key1"=>$myStr1,
    "key2"=>$myStr2
);

echo($myArray["key1"]);

To produce this:

"sometext"

Instead of this:

"1"        // why??

Any help would be appreciated.

EDIT:

The above works; my bad. Here's the real problem - my $myStr1 variable isn't just assiged a string literal like in the above; it's created using the following syntax:

$myStr1 = "sometext" + anObject->intProperty + "moretext";

Basically I use the + to concatenate various types into a string. Maybe + isn't doing what I think it's doing?

EDIT:

It was definitely the + operator. I casted all non-strings to strings and used . to concatenate instead.

share|improve this question
5  
ideone.com/ywwma --- your code works as expected –  zerkms Jul 19 '12 at 2:07
 
Crap. Well I guess I have some investigative work to do. Thanks for pointing this out. –  Nathan Friend Jul 19 '12 at 2:12
 
What PHP version are you using? –  uınbɐɥs Jul 19 '12 at 2:16
 
@ShaquinTrifonoff I'm using 5.4.3. –  Nathan Friend Jul 19 '12 at 2:21
1  
For the record the + operator in PHP only performs additions, and if you use it with strings it will just try to cast the strings to values (1) and add them. (just in case it was still unclear) –  Mahn Jul 19 '12 at 3:20
show 3 more comments

2 Answers

up vote 1 down vote accepted

You've got it correct the first time. Try this:

$myStr1 = "sometext";
$myStr2 = "someothertext";

$myArray = array(
    "key1"=>$myStr1,
    "key2"=>$myStr2
);

unset($myStr1);

echo($myArray["key1"]);

Even though we unset() the $myStr1 variable, it still echoed sometext.

It should be noted that while it is possible to set $myStr1 by reference, it's not the default.

share|improve this answer
 
Your answer is the same as the other one, plus the comment. –  uınbɐɥs Jul 19 '12 at 2:15
 
&"sometext"; - it's not valid php. Only variables can be passed by reference –  zerkms Jul 19 '12 at 2:16
 
@ShaquinTrifonoff in other words, I provide an actual answer, instead of a comment in answer form? –  SomeKittens Jul 19 '12 at 2:17
 
@zerkms noted, thanks. Momentary brain lapse. It's fixed now. –  SomeKittens Jul 19 '12 at 2:17
add comment

Try your code and its result is:

sometext
share|improve this answer
1  
Even though this may be considered as an answer, I still personally think it would be more suitable to leave it as a comment –  zerkms Jul 19 '12 at 2:11
add comment

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.