Solved

php 5.3 array_rand() not working?

Posted on 2010-09-14
5
756 Views
Last Modified: 2012-05-10
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
Comment
Question by:Loganathan Natarajan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33671167
"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
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 125 total points
ID: 33671275

<?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
 
LVL 36

Author Comment

by:Loganathan Natarajan
ID: 33671386
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33672062
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
 
LVL 36

Author Closing Comment

by:Loganathan Natarajan
ID: 33679088
thanks, it worked out
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question