Jump to content

Archived

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

GeorgeMoney

Arrays

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.....

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

In regards to the randomizing the words...try:

[code]<?php

$names = array('Charles Manson', 'Adolf Hitler', 'Ted Bundy', 'George Bush');
$randomiz0r = $names[rand(0, count($names) -1)];

echo"Why hello there, $randomiz0r";

?>[/code]

Share this post


Link to post
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; ?>" />

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites
I am making a random sentence generator thingy using my limited knowledge of php.

I guess I have been confusing everyone.

I think I have it working now, i just can't fix all the minor tiny bugs.

Share this post


Link to post
Share on other sites
Try
[code]<?php
foreach ($words as $key => $value) {
    $$key = explode(";", $value);        // <-- note $$
}
// view new arrays
echo '<pre>', print_r($people, true), '</pre>';
echo '<pre>', print_r($foo, true), '</pre>';
?>[/code]

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.