purefusion Posted March 16, 2006 Share Posted March 16, 2006 I swear I did this before, using some method or another. I need to execute code similar to this:[code]<? $username = $_POST['assoc']; $name = $user['$username']; echo "$name";?>[/code]Simple enough. Also, is there a way to reverse lookup an array value from a key, or a key from a value?Thanks! Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted March 16, 2006 Share Posted March 16, 2006 [code]<? $username = $_POST['assoc']; $name = $user['$username']; // sure this will work? maybe "$username" // that woudl give you $user[$_POST['assoc']] - I think anyway. echo "$name";?>[/code]Look for the array_keys function. Quote Link to comment Share on other sites More sharing options...
txmedic03 Posted March 17, 2006 Share Posted March 17, 2006 [code]<? $username = $_POST['assoc']; $name = $user[$username]; echo $name;?>[/code]Over use of quotes where not needed and if I'm not mistaken the single quotes don't process variables inside of them. If you would like a more indepth explanation, let me know. Quote Link to comment Share on other sites More sharing options...
Steveo31 Posted March 17, 2006 Share Posted March 17, 2006 medic's right. Single quotes won't work, but it's generally a good idea to use double quotes when dealing with variables inside arrays. If the value of the variable is a number, it could be either parsed as a key location, or a value. But as for what you need to do, make sure you are defining "$user" as an array before pushing somethine into it. Also, wouldn't you need to do[code]$user = array();$username = $_POST['assoc'];$user[] = $username;$name = $user["$username"];echo $name;[/code]What is the value of $_POST['assoc']? What kind of array are you trying to make? Quote Link to comment Share on other sites More sharing options...
purefusion Posted March 17, 2006 Author Share Posted March 17, 2006 I tried it with single quotes, double quotes and no quotes to no avail before I posted the topic. Nothing seemed to work.the array will contain a value "Full Name" from a key of "flastname" (first initial and lastname). But the form will only send the key as the variable, not the value. It's chosen from a drop down list. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 17, 2006 Share Posted March 17, 2006 You are probably looking for [a href=\"http://us2.php.net/manual/en/language.variables.variable.php\" target=\"_blank\"]variable variables[/a].[code]<? $username = $_POST['assoc']; $name = ${$user[$username]}; echo $name;?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
purefusion Posted March 20, 2006 Author Share Posted March 20, 2006 wow, none of those worked. I extracted a small amount of code to run the query outside of the main script it was located in:[code]<?$user = array ('jallen' => 'Jim Allen', 'mmessmer' => 'Mike Messmer', 'sshowalter' => 'Scott Showalter', 'dmartin' => 'Dean Martin', 'krea' => 'Kathy Rea', 'mshaffer' => 'Mike Shaffer', 'kjenkins' => 'Ken Jenkins', 'webmaster' => 'Annonymous Someone');$username = 'mshaffer';$name = $user[$username];echo $name;?>[/code]And as it turns out, the no-quotes method works fine. The problem I found was in that the part of the script generating the $name variable came [i]after[/i] the script calling it. Doh! But this script is huge, and so it was hard to see that at first.Thanks for coming together with all sorts of ideas. I definately learned something new! (the variable variables concept, with may come in handy down the road. :-DThanks again! Quote Link to comment Share on other sites More sharing options...
txmedic03 Posted March 21, 2006 Share Posted March 21, 2006 I'm just glad everything turned out well for you in the end. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.