Rand Array is not assign the chosen value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Rand Array is not assign the chosen value

Post 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]
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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?
(#10850)
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Wheres `$PercentageResults` coming from?

And what are you echoing?

var_dump($PercentageKey) To see what you are getting from array_rand.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

You are right ... it does return keys. I stand corrected.
(#10850)
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post 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.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post 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.
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post by SirChick »

I shall give it a bash and let you know if it works.
Post Reply