[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 808
  • Last Modified:

php 5.3 array_rand() not working?

I have php 5.3 version hosting and trying to achieve the result from array_rand().. it is not working....

the below example is not displayed randomly item ...but it works in php 5.2 version, i checked it....


<?php
	$inventory_Subtitle = array('60 DAY ; Both Ways!  Cover %  Returns!',
										 'PAY LESS;  ! 100% Natural Diamonds!',
										 'FREE Shipping;  Ways! Enter to  Out More!',
										 ': These Diamonds are 100%  & NON !',
										 'Highest Quality;  Prices! .',
										 '100% ; HIGH  NON Enhanced Diamonds!',
										 'BEST  on  for 100% Natural !',
										 'BEST  for  Natural !  Us.');
										 
			$rand_keys 		= array_rand($inventory_Subtitle,8);
			
			
			echo $inventory_Subtitle[$rand_keys[0]];
									 	

?>

Open in new window

0
Loganathan Natarajan
Asked:
Loganathan Natarajan
  • 3
  • 2
1 Solution
 
Ray PaseurCommented:
"is not working" does not give us much to go on if we are going to try to help you.   What happens?  Loop?  Wait?  Incorrect out?  Halt and catch fire?
0
 
Ray PaseurCommented:

<?php // RAY_temp_logudotcom.php
error_reporting(E_ALL);
echo "<pre>";

// TEST DATA
$inventory_Subtitle
= array
( '60 DAY ; Both Ways!  Cover %  Returns!'
, 'PAY LESS;  ! 100% Natural Diamonds!'
, 'FREE Shipping;  Ways! Enter to  Out More!'
, ': These Diamonds are 100%  & NON !'
, 'Highest Quality;  Prices! .'
, '100% ; HIGH  NON Enhanced Diamonds!'
, 'BEST  on  for 100% Natural !'
, 'BEST  for  Natural !  Us.'
)
;

// SEE MAN PAGE: http://us.php.net/manual/en/function.array-rand.php
$rand_keys      = array_rand($inventory_Subtitle,8);
var_dump($rand_keys);

// SEE MAN PAGE: http://us.php.net/manual/en/function.shuffle.php
shuffle($rand_keys);
var_dump($rand_keys);

// SHOW THE RANDOM KEY
echo $inventory_Subtitle[$rand_keys[0]];

Open in new window

0
 
Loganathan NatarajanLAMP DeveloperAuthor Commented:
it always displays only one value .. i. e **60 DAY ; Both Ways!  Cover %  Returns!**

it does not return random values ... i mean another value etc.,
0
 
Ray PaseurCommented:
The code I posted returns random values.  It is not a PHP 5.3 problem.  Please check the man pages in the comments section of the code snippet.

best regards, ~Ray
0
 
Loganathan NatarajanLAMP DeveloperAuthor Commented:
thanks, it worked out
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now