Jump to content

Dynamic Arrays


purefusion

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!
Link to comment
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.
Link to comment
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.
Link to comment
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?
Link to comment
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.
Link to comment
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
Link to comment
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!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.