Jump to content

Arrays


GeorgeMoney

Recommended Posts

When I try to call an array using a variable as the key's name it doesn't work. For example, $foo[$bar] doesn't work.

Would there be any way to make this work?

More specifically I want to make a random word thing.

This code doesn't work, is there any way to make it work?
[code]
<?php
$arr = array(
'foo',
'bar',
'foobar'
)

$rand = $arr[rand(0, count($arr))];

echo "Hello, $rand";

?>[/code]
.

And if I am able to get this to work, I would like to also make a way for a form to be able to get from the array, eg:
[code]
<?php
$username = $_POST['username'];
if(isset($arr[$username])) {
echo "Welcome. Your name exists in our list!";
} else {
?>
<form method="post">
<input name="username">
<input type="submit">
</form>
<?php
}
?>
[/code]

So can anyone help me with this? It has to be possible... I guess.....
Link to comment
Share on other sites

Update:
I just read a little more in the php manual and it says:
[quote]
As stated in the syntax section, there must be an expression between the square brackets ('[' and ']'). That means that you can write things like this:

<?php
echo $arr[somefunc($bar)];
?>
[/quote]
.

If so then why is it not working for me?
Link to comment
Share on other sites

you cant use a variable as the key because your keys are numeric. your array would look like this
[code]$arr = array(
'foo',
'bar',
'foobar'
) would translate to

[0]=>foo
[1]=>bar
[2]=>foobar
[/code]
0,1,2 are the keys




for your method of fililng in the form, why not use a simple ternary operator

$uname = (trim($_POST['username']) != "") ? $_POST['username'] : "";

(condition) ? if true : if false;

<input type="text" value="<?php echo $uname; ?>" />
Link to comment
Share on other sites

Yes, thanks guys, you helped alot! The new problem is that I want to use a foreach to explode() the values in the main array, for example (I changed what I was supposed to do in the original post, because I found a different way then what I said earlier):

[code]<?php
$words = array(
"people" => "me;you;us;joe",
"foo" => "bar;foo;someone;yep"
);
?>
[/code]

So what I want to do is turn each key name into the name of the new array and the values seperated by a ";".

For example, this doesn't work:
[code]foreach ($words as $key => $value) {
$key = explode(";", $value);
}[/code]

so I want for example a new array called $foo and another array called $people with the values as "me", "you", etc.....

I'm pretty sure theres a way to do this but i'm not doing it right. Any help would be appreciated!
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.