Hello-
This is probably easy to do, but I am at a loss.
I have 6 separate arrays that generate a random number.
I want a single array that contains the 6 numbers generated.
Or if there is a simpler way to do this, please let me know.
Hello-
This is probably easy to do, but I am at a loss.
I have 6 separate arrays that generate a random number.
I want a single array that contains the 6 numbers generated.
Or if there is a simpler way to do this, please let me know.
if you have 6 arrays and you want to put all that to one array you do like below:
$arrResult[]= $arrrand1;
$arrResult[]= $arrrand2;
$arrResult[]= $arrrand3;
$arrResult[]= $arrrand4;
$arrResult[]= $arrrand5;
$arrResult[]= $arrrand6;
Or you can combine the arrays using array_merge http://in2.php.net/manual/en/function.array-merge.php
Thanks for the reply, it doesn’t work.
$arrResult[]= $arrrand1;
$arrResult[]= $arrrand2;
$arrResult[]= $arrrand3;
$arrResult[]= $arrrand4;
$arrResult[]= $arrrand5;
$arrResult[]= $arrrand6;
gives back “array” with no numbers.
array_merge and array_combine doesn’t seem to work with numbers.
to get the results from an array try one of these two methods.
//method one
print_r($arrResult);
echo “
”;
//method two
foreach($arrResult as $key => $value){
echo “[$key] => [$value]” . “
”;
}
Thanks for the help, however I was able to get the result I wanted using implode.
here is a little something I don’t know how your generating the numbers but I hope this makes life a little simpler implode requires an explode to use any variable and this way you do not need that step
[php]<?php
for ($i=0;$i <= 5;$i++) {
$arr[] = $rand = rand(0,1000); //creates and array of 6 random numbers from 0 to 1000
}
foreach ($arr as $key => $value) {
echo $value."
"; //echos each value of the array
}
echo “
”;
echo $arr[0]."
"; //echos the first value in the array
echo $arr[1]."
"; //echost the second value in the array and so on…
echo $arr[2]."
";
echo $arr[3]."
";
echo $arr[4]."
";
echo $arr[5]."
";
?>[/php]
You do not need to use “explode” to access an “imploded” array. The imploded array is just an array.
It can be accessed as any other array. Depending on how you create the imploded array, you may need
to use the format $key=>$value to retrieve the data. But, you do not need to use the explode function.
Hello!
I’m just getting my feet wet with php. Like, I’ve just dipped my toes in…Andrew, I don’t know what that script says, lol, the $i++ whatever… haven’t got that far into understanding those commands…
Right now I have 6 $array commands or whatever that each generate a number between 0 and 9.
I have found, that when using a script similar to yours, it won’t repeat the random number it chooses, which is something I
need it to do. I’m working on an advanced lottery number picker type script.
$i++; Means add one to $i… It is the same as $i=$i+1;
The ++ AFTER the variable means to use the variable, then increment it.
The ++ BEFORE the variable means to increment it and then use it.
All of this is in both php.net/manual/ and w3schools.com/php/ sites. They both have tutorials on these and all other things PHP…
Hope that helps!
Hello Ernie-
Thanks for the reply. I have come across this on both those sites, however, because I don’t understand the functions, I don’t feel comfortable using them.
That doesn’t mean I won’t ever use it, just not yet. This php stuff is like learning french. I only use the parts I understand. lol.
I was using ‘IF THEN’ statements until I understood what was happening with switches. Once I ‘got’ switches, I shaved about 200 lines of code from my script.
Nice to save 200 lines of code!
by the way, there are extremely nice tutorials on the w3shools site.
Just search for the correctly thing. They are clear and simple to understand…
Good luck and let us know if you need further help…
Thanks.
The only problem I have now is that I want the script to compare the number generated to a list of other numbers to see if there is a match.
I may not have explained that correctly.
I have 6 arrays that generate a random number from 0 to 9. Like throwing 6 dice.
So let’s say the numbers generated were 1-2-3-4-5-6.
I used implode so now I have one six digit number 123456… or not… not sure if it just LOOKS that way.
I want to now have the script check to see if 123456 matches a six digit number in another array with the IN_ARRAY command.
It’s not working so I’m guessing I missed something somewhere. I’m guessing that IMPLODE didn’t combine the numbers into one six digit number really.
Well, if you set up the implode correctly, and each number in the array is a number, you may have an issue.
Implode is actually more for text uses. So, the number 1 is not the text number “1”. For instance,
an array of 0,3,0,3 would show as 303 not 0303. If all the numbers are non-zero, then it should work.
And, if you try to fix the zero issue but using 1 to 10, there would be other issues. (1,0 same as 0,10)
So, I might suggest using text values instead. You can use a delimiter such as a dash. “-”
This would give 0,3,0,3 something like 0-3-0-3 and since it would be text and a set length, 6 numbers, 5 dash,
it would be easy to compare and whatever.
Or, you could just keep the array as an array and don’t bother combining them and just do an array compare
instead. This would be faster as you would just compare the arrays without any conversions…
I guess this brings us to other questions… How are you keeping the data stored? In a file, database or is it
just a one-run program with no numbers stored for later?
If you are storing it in a file, just write the array as-is. If you are using a database just store the array as-is.
If you are using a one-run program, just create a new array holding the numbers arrays. You can make
arrays of arrays just as easy as making the original array!
Andrew showed you how to create an array of random numbers. So creating one number is not an issue.
You can create an array of an array like this… Let’s say your array is call $rand_array and that it currently
has 6 random numbers store in it. To create a new array for storing this number you would do something
like: $rand_storage_array = array($rand_array); This will create a new array with the current values
of the first array inside it. Then, you create another one like: $rand_storage_array[]=$new_array_to_store;
To read them stored ones back, loop thru them from $rand_storage_array[0] to the last one which is
$rand_storage_array[count($rand_storage_array)]. In a for loop, use 0 and the count…
Remember when you use the data inside of $rand_storage_array, it is an ARRAY! So, once you pull a value
from, well, like this: $temp=$rand_storage_array[23]; The $temp will actually become an array with the
6 values of numbers inside it. So, $temp[0], $temp[1]…$temp[6]
Hope that makes sense… Not sure on how your code is laid out, so hope that this helps!
I followed about a third of that lol.
It’s a simple script actually.
6 arrays using mt_rand(0,9)
I use switches to compare each array result to another number and then generate next array number … so there is a minimum number and maximum number. There is no 111111 or 999999.
like array1 = mt_rand(1,9)
check to see if array = x generate second number.
check to see if array = y generate second number.
echo first number
check to see if array2 = x generate third number.
check to see if array2 = y generate third number.
echo second number
And so on.
and so on…
The end result is 6 different numbers… which may or may not repeat.
I need to turn it into one 6 digit number and then compare that number to an array of employee id numbers to see if there is a match.
Okay, perhaps I am not clear on this…
So, you have a list of employee numbers? Correct?
Where are they stored?
You want to pick 6 random numbers and then see if they match the employee list of numbers? Correct?
Where is your list of employee numbers stored. Most likely you are way over-coding this…
Also, if you pick a 6 digit number, what happens if it is not in the employee list of numbers?
Do you just pick another 6 digit number?
The employee numbers are in their own array. I am not skilled enough to use php with a database yet.
Yes. Pick 6 numbers, check for a match, if no match, then no match… for now. I double check every random result.
I’m running it as a lottery type game. You may or may not win.
I am also using a weighted rand. Depending on the result of that, the script will pick 6 random numbers, or a random employee number.
I don’t know how to make it rerun to pick new numbers aside from refreshing the page. :-X
You said:
I don’t know how to make it rerun to pick new numbers aside from refreshing the page.
So, your code picks the numbers, 6 of them and places them into an array. Then you do some odd compares and check other numbers and finally do some implode to create a 6 digit number and compare it to employees array… Still silly from what you have told me. Maybe I am still mixed up on what you want to do.
If you want to pick a random 6 digit number, just do $num=rand(111112,99998); (You said there was not any 1111111 or 999999… So, that would create a random number between 111112 and 999998. Then, you just check the employee array for an entry… If(isset(emp_array[$num]))…etc… If the employee array for that random number ($num) is valid and contains info, then, you can continue. If that entry is empty, then there is no employee with that number… Is that more of what you are looking for?
Thanks for all your help.
I’m sure I over complicated this script, but it helps me to understand the functions. As I said before, when I figure things out, I rewrite the sections.
So, originally I used RAND with a range between 108832 and 642096. This is the lowest id number to the highest. Not every number in this range is an active employee however.
This makes it fun, because as a lottery game, there is a smaller range to select from, but also a lot of additional inactive numbers that can pop up.
As a raffle game, I can change some of the code so it just picks a random number from the employee id array instead. That way there is a guaranteed winner.
Why did I complicate the code with switches and implodes and such?
Well… I found an old php lottery game script that is automated with a timer and displays the winning numbers as balls… which is just a gif image obviously.
The 6 digit id numbers are a single number, versus 6 individual numbers, so they won’t display properly in the ball image.
As 6 separate numbers, they can be displayed in the ball images, and as a single 6 digit number, I can them check the id array for a match.
Again, I’m sure I’ve over complicated this since I’m just learning php, and there is probably a WAY simpler way to do it, but I’m enjoying figuring out how it all works.