Jump to content


Photo

Dynamic Arrays


  • Please log in to reply
7 replies to this topic

#1 purefusion

purefusion
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 16 March 2006 - 09:59 PM

I swear I did this before, using some method or another. I need to execute code similar to this:

<?
  $username = $_POST['assoc'];
  $name = $user['$username'];
  echo "$name";
?>

Simple enough.

Also, is there a way to reverse lookup an array value from a key, or a key from a value?

Thanks!

#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 16 March 2006 - 10:10 PM

<?
  $username = $_POST['assoc'];
  $name = $user['$username']; // sure this will work? maybe "$username"
                                              // that woudl give you $user[$_POST['assoc']] - I think anyway.
  echo "$name";
?>


Look for the array_keys function.
follow me on twitter @PHPsycho

#3 txmedic03

txmedic03
  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 17 March 2006 - 01:09 AM

<?
  $username = $_POST['assoc'];
  $name = $user[$username];
  echo $name;
?>

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.

SEMPER FIDELIS!

I can't stop you from doing something silly, but at least I can help you do it right.


#4 Steveo31

Steveo31
  • Members
  • PipPip
  • Member
  • 25 posts
  • LocationSan Jose, CA

Posted 17 March 2006 - 01:15 AM

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

$user = array();

$username = $_POST['assoc'];
$user[] = $username;

$name = $user["$username"];
echo $name;

What is the value of $_POST['assoc']? What kind of array are you trying to make?

#5 purefusion

purefusion
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 17 March 2006 - 02:09 PM

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.

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 17 March 2006 - 03:04 PM

You are probably looking for [a href=\"http://us2.php.net/manual/en/language.variables.variable.php\" target=\"_blank\"]variable variables[/a].

<?
  $username = $_POST['assoc'];
  $name = ${$user[$username]};
  echo $name;
?>

Ken

#7 purefusion

purefusion
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 20 March 2006 - 09:23 PM

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:


<?
$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;
?>

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 after 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. :-D

Thanks again!

#8 txmedic03

txmedic03
  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 21 March 2006 - 07:04 AM

I'm just glad everything turned out well for you in the end.

SEMPER FIDELIS!

I can't stop you from doing something silly, but at least I can help you do it right.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users