GeorgeMoney Posted September 6, 2006 Share Posted September 6, 2006 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..... Quote Link to comment Share on other sites More sharing options...
GeorgeMoney Posted September 7, 2006 Author Share Posted September 7, 2006 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:<?phpecho $arr[somefunc($bar)];?> [/quote]. If so then why is it not working for me? Quote Link to comment Share on other sites More sharing options...
Caesar Posted September 7, 2006 Share Posted September 7, 2006 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] Quote Link to comment Share on other sites More sharing options...
emehrkay Posted September 7, 2006 Share Posted September 7, 2006 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 keysfor 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; ?>" /> Quote Link to comment Share on other sites More sharing options...
GeorgeMoney Posted September 7, 2006 Author Share Posted September 7, 2006 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! Quote Link to comment Share on other sites More sharing options...
emehrkay Posted September 7, 2006 Share Posted September 7, 2006 george, if you dont mind me asking, what exactly are you doing? Quote Link to comment Share on other sites More sharing options...
GeorgeMoney Posted September 7, 2006 Author Share Posted September 7, 2006 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 7, 2006 Share Posted September 7, 2006 Try[code]<?phpforeach ($words as $key => $value) { $$key = explode(";", $value); // <-- note $$}// view new arraysecho '<pre>', print_r($people, true), '</pre>';echo '<pre>', print_r($foo, true), '</pre>';?>[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.