Page 1 of 1

Rand Array is not assign the chosen value

Posted: Wed Oct 10, 2007 6:48 pm
by SirChick
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have a random array and when i echo the value that the array choose it seems to be empty..... I'm not sure why.. i purposely only put one choice for testing purposes and it still won't do it.... this is what i have:

Code: Select all

$PercentageAddOn = array();
				//$PercentageAddOn[] = 2.62;
				//$PercentageAddOn[] = 52421;
				//$PercentageAddOn[] = 51;
				$PercentageAddOn[] = 1.32;
				//$PercentageAddOn[] = 4.72;
				//$PercentageAddOn[] = 9.72;
				//$PercentageAddOn[] = 0.2;
				//$PercentageAddOn[] = 0.72;
				//$PercentageAddOn[] = 0..9;
			$PercentageKey = array_rand($PercentageAddOn);
			$PercentageAddOn = $PercentageResults[$PercentageKey];
The result should be $PercentageAddOn[] = 1.32; but when echo'd it doesnt show anything....Any one know why?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Oct 10, 2007 7:36 pm
by Christopher
array_rand() does not return a key, it returns one or more values from the array. And why are you assigning an single element to the whole array var?

Posted: Wed Oct 10, 2007 7:50 pm
by Zoxive
Wheres `$PercentageResults` coming from?

And what are you echoing?

var_dump($PercentageKey) To see what you are getting from array_rand.

Posted: Wed Oct 10, 2007 10:37 pm
by Stryks
arborint ... are you sure about array_rand.

I read this post and it happened to show up in a reply I was doing elsewhere. Tested, and sure enough, it appears to return either a single key or an array of keys, not array values.

Has it always done that? I'm sure I remember it returning values directly.

Posted: Thu Oct 11, 2007 12:57 am
by Christopher
You are right ... it does return keys. I stand corrected.

Posted: Thu Oct 11, 2007 8:13 am
by SirChick
It did return values but now it doesn't return anything.. i haven't changed it since i used it in a different script which works perfectly.. yet this one doesn't


Not entirely sure why still though.

Posted: Thu Oct 11, 2007 8:24 am
by Stryks
Not really sure where the $PercentageResults is coming from on the last line though.

You haven't meant to type $PercentageAddOn have you?

Code: Select all

$PercentageAddOn = array();
//$PercentageAddOn[] = 2.62;
//$PercentageAddOn[] = 52421;
//$PercentageAddOn[] = 51;
$PercentageAddOn[] = 1.32;
//$PercentageAddOn[] = 4.72;
//$PercentageAddOn[] = 9.72;
//$PercentageAddOn[] = 0.2;
//$PercentageAddOn[] = 0.72;
//$PercentageAddOn[] = 0.9;

$PercentageKey = array_rand($PercentageAddOn);
$PercentageAddOn = $PercentageAddOn[$PercentageKey];  

print_r($PercentageAddOn);
Works for me. Of course I might be misunderstanding what you are trying to do.

Posted: Thu Oct 11, 2007 8:26 am
by SirChick
I shall give it a bash and let you know if it works.