arrays and random number generators.

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
shehan31
Forum Commoner
Posts: 59
Joined: Sun Aug 29, 2010 5:24 am

arrays and random number generators.

Post by shehan31 »

Hi all;
I am trying to work out to put the contents of a variable into an array(string array) and to generate a random number from it.
Ex : if i type "ABCA " as the input in the message box the out put has to be "BCDB". But the output shows as BBCD(looks liketo an alphebatical order).
I tried to eleminate that by assinging the contents of the value of the variable $value into an array and they 'krsort 'it. It gives me warnings.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Warning: krsort() expects parameter 1 to be array, string given in C:\wamp\www\pin project\Codelobster1.php on line 16
B
Warning: array_rand() expects parameter 1 to be array, string given in C:\wamp\www\pin project\Codelobster1.php on line 19

Warning: krsort() expects parameter 1 to be array, string given in C:\wamp\www\pin project\Codelobster1.php on line 16
B
Warning: array_rand() expects parameter 1 to be array, string given in C:\wamp\www\pin project\Codelobster1.php on line 19

Warning: krsort() expects parameter 1 to be array, string given in C:\wamp\www\pin project\Codelobster1.php on line 16
C
Warning: array_rand() expects parameter 1 to be array, string given in C:\wamp\www\pin project\Codelobster1.php on line 19

Warning: krsort() expects parameter 1 to be array, string given in C:\wamp\www\pin project\Codelobster1.php on line 16
D
Warning: array_rand() expects parameter 1 to be array, string given in C:\wamp\www\pin project\Codelobster1.php on line 19
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Code: Select all

<?php
if (isset($_POST['submit1']))
   { 
   $message = $_POST['Message'];
//$array1=array('A','C','B','A');
	$str= str_split($message);
	
	$array = array('A'=>'B','B'=>'C','C'=>'D');

foreach($array as $key=>$value){
	for($i=0;$i<count($str);$i++){
		if($key==$str[$i]){
	   //assigns the contents of the value variable into an array. 
		 $value==array();
		 
		 krsort($value);
		 echo"$value"; 
		 //generates a random number;
		 $random = array_rand($value);
		 echo"$random";
    
		}
		
	}
}
}

   
echo"

 <form action='Codelobster1.php' method='POST' >
 <td>
   <tr>
        <td>
		
         <font SIZE = 5 FACE = times new roman >Message</font>
        </td>
        <td>
        <input type='text' name='Message' maxlength='30'>
        </td>
   </tr>
   </td>
    <tr>
       <td align='center' valign='top' colspan='2'>
        <input type='submit' name='submit1'  value='Confirm'>
       </td>
	   </tr>
		</form>";
	
?>
Regards
Shehan31
mellowman
Forum Commoner
Posts: 62
Joined: Sat Nov 22, 2008 5:37 pm

Re: arrays and random number generators.

Post by mellowman »

This has been done before and there is a great tutorial about it :D

http://www.youtube.com/watch?v=EUBr-aHJzvw
shehan31
Forum Commoner
Posts: 59
Joined: Sun Aug 29, 2010 5:24 am

Re: arrays and random number generators.

Post by shehan31 »

mellowman wrote:This has been done before and there is a great tutorial about it :D
hi mellowman;
thank you for the reply. It helps me to some level but still i am looking for a method to store the contents of a variable in to an different arry. Therfore I can sort the order.
http://www.youtube.com/watch?v=EUBr-aHJzvw
Post Reply