yhbae Posted December 9, 2009 Share Posted December 9, 2009 Hi guys, I have encountered a problem while coding in PHP. Here's the problem: I've used an associative array throughout my code and in some cases, the key of the array contains space. I do realize that if you surround your keys with " this should work fine. Except - I cannot do this because I use variables instead. Here's an example: $fish1 = "Pseudotropheus Acei"; $fish2 = "Pseudotropheus Demasoni"; $size[$fish1] = 5; $size[$fish2] = 6; The problem is that 'size' array will not distinguish between the keys $fish1 and $fish2. It _appears_ to only take the first word "Pseudotropheus" hence presumably mapping both into the same space. Does anyone have any ideas? I tried so far: $size["$fish1"] = 5; $size['$fish1'] = 5; $size["'"$fish1"'"] = 5; $size["\""$fish1"\""] = 5; None of the above works. Other than replacing the space with say '_' (which isn't ideal), I am running out of ideas. Appreciate some feedback. Link to comment https://forums.phpfreaks.com/topic/184478-associative-array-using-variable-in-a-key-that-contains-a-space/ Share on other sites More sharing options...
jjacquay712 Posted December 9, 2009 Share Posted December 9, 2009 You should be able to do this: eval("$size[\"" . $fish1 . "\"]"); Link to comment https://forums.phpfreaks.com/topic/184478-associative-array-using-variable-in-a-key-that-contains-a-space/#findComment-973852 Share on other sites More sharing options...
yhbae Posted December 9, 2009 Author Share Posted December 9, 2009 You should be able to do this: $size["\"" . $fish1 . "\""] You should also use print_r() to debug. Tried that one too, and didn't work for me. Should this work? May be I overlooked something else... Link to comment https://forums.phpfreaks.com/topic/184478-associative-array-using-variable-in-a-key-that-contains-a-space/#findComment-973853 Share on other sites More sharing options...
jjacquay712 Posted December 9, 2009 Share Posted December 9, 2009 You should be able to do this: eval("$size[\"" . $fish1 . "\"]"); I just figured out you can use eval. Link to comment https://forums.phpfreaks.com/topic/184478-associative-array-using-variable-in-a-key-that-contains-a-space/#findComment-973854 Share on other sites More sharing options...
yhbae Posted December 9, 2009 Author Share Posted December 9, 2009 You should be able to do this: eval("$size[\" " . $fish1 . "\"]"); I just figured out you can use eval. Can you please elaborate? Thanks. Link to comment https://forums.phpfreaks.com/topic/184478-associative-array-using-variable-in-a-key-that-contains-a-space/#findComment-973855 Share on other sites More sharing options...
jjacquay712 Posted December 9, 2009 Share Posted December 9, 2009 What eval does is parse the string you input as if it was in the code. eval("echo 'Hello World';"); // Would print out Hello World. So by doing this: eval("$size[\" " . $fish1 . "\"]"); PHP would parse this string: "$size[\"" . $fish1 . "\"]" Then evaluate it as if it were code. Sorry if my explanation is confusing, it's the best i can do. Link to comment https://forums.phpfreaks.com/topic/184478-associative-array-using-variable-in-a-key-that-contains-a-space/#findComment-973859 Share on other sites More sharing options...
salathe Posted December 9, 2009 Share Posted December 9, 2009 There should be no problem at all using keys with spaces. Run the following and show us the output: $fish1 = "Pseudotropheus Acei"; $fish2 = "Pseudotropheus Demasoni"; $size[$fish1] = 5; $size[$fish2] = 6; var_dump($size); Link to comment https://forums.phpfreaks.com/topic/184478-associative-array-using-variable-in-a-key-that-contains-a-space/#findComment-973860 Share on other sites More sharing options...
yhbae Posted December 9, 2009 Author Share Posted December 9, 2009 Thanks guys, got it working now. Link to comment https://forums.phpfreaks.com/topic/184478-associative-array-using-variable-in-a-key-that-contains-a-space/#findComment-973885 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.