Jump to content
Updating IPB tonight Read more... ×

Archived

This topic is now archived and is closed to further replies.

purefusion

Dynamic Arrays

Recommended Posts

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!

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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. :-D

Thanks again!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.