Jump to content


Photo

Arrays


  • Please log in to reply
7 replies to this topic

#1 GeorgeMoney

GeorgeMoney
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 06 September 2006 - 11:47 PM

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?
<?php
$arr = array(
'foo',
'bar',
'foobar'
)

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

echo "Hello, $rand";

?>
.

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:
<?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
}
?>

So can anyone help me with this? It has to be possible... I guess.....
--GeorgeMoney

#2 GeorgeMoney

GeorgeMoney
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 07 September 2006 - 12:07 AM

Update:
I just read a little more in the php manual and it says:

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)];
?>

.

If so then why is it not working for me?
--GeorgeMoney

#3 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 07 September 2006 - 12:53 AM


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

<?php

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

echo"Why hello there, $randomiz0r";

?>

PHP Ninja

#4 emehrkay

emehrkay
  • Staff Alumni
  • Advanced Member
  • 1,214 posts

Posted 07 September 2006 - 01:24 AM

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

[0]=>foo
[1]=>bar
[2]=>foobar
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; ?>" />

#5 GeorgeMoney

GeorgeMoney
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 07 September 2006 - 01:32 AM

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):

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

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:
foreach ($words as $key => $value) {
$key = explode(";", $value);
}

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!
--GeorgeMoney

#6 emehrkay

emehrkay
  • Staff Alumni
  • Advanced Member
  • 1,214 posts

Posted 07 September 2006 - 01:36 AM

george, if you dont mind me asking, what exactly are you doing?

#7 GeorgeMoney

GeorgeMoney
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 07 September 2006 - 02:01 AM

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

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 07 September 2006 - 10:30 AM

Try
<?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>';
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users