Page 1 of 1

Array Imploding erasing duplicates...?[SOLVED]

Posted: Wed Sep 05, 2007 8:31 am
by MikeCXT
Here is my short code for generating a random coupon number.

Code: Select all

$convert_array = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z","1","2","3","4","5","6","7","8","9");

for ($i=0;$i<=1;$i++) {
  for ($j=0;$j<=5;$j++) {
	$before_array[] = rand (0, 33);
  }
  foreach ( $before_array as $x ) {
    $y[$x] = $convert_array[$x];
	echo "$y[$x]";
  }     
$coupon = implode(",",$y);
echo"<br /><b>$coupon</b><br /><br />";
This gives me a 6 digit coupon. The problem, is that when I echo "$y[$x]";, in the middle of the code, I get the correct 6 digits. However, they are seperate, and to input them into sql later, I want to combine them into 1 string. So I found Array Implode. When I use it though, it does implode my array into 1 string, but any duplicate numbers/letters are lost. If my original echo "$y[$x]"; showed C59Q5B, then my echo after the implode reveals the new string as C59QB... the 5 between the Q and B has disappeared. This happens to any letter or number that occurs more than once. I looked at php.net/manual and could not find why this happens. NOTE-The comma's in the implode are not needed, just using them for visual easy for now.

Is Implode working as it is suppose to? Is there a way to use implode and not have it do what it is here? Or is there a better way to go about doing this. I'm just generating 6 random numbers, converting those to numbers or letters (excluding 0 and O), and then merging them into 1 string so I can input it into SQL. Thank you for any and all help.


-Mike-

Posted: Wed Sep 05, 2007 8:41 am
by CoderGoblin
Does this code do what you are after ?

Code: Select all

$convert_array = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z","1","2","3","4","5","6","7","8","9");
$coupon='';
$max_choice=count($convert_array)-1; // inserted here in case you ever change the convert array (to remove 1 and I for instance)
for ($j=0;$j<=5;$j++) {
  $coupon.=$coupon[rand (0, $max_choice)];
}
Edit:: corrected $max_choice

Re: Array Imploding erasing duplicates...?

Posted: Wed Sep 05, 2007 8:44 am
by superdezign
Can't you just use a string instead of an array?

EDIT: CoderGoblin's code does just that.

Posted: Wed Sep 05, 2007 8:45 am
by VladSun
A little bug fix for CoderGoblin's code :)
It should be:

Code: Select all

$coupon.=$convert_array[rand (0, $max_choice)];

Posted: Wed Sep 05, 2007 8:57 am
by CoderGoblin
Oops... :oops:

Posted: Wed Sep 05, 2007 9:21 am
by stereofrog
Another approach

Code: Select all

$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$coupon = substr(str_shuffle($chars), 0, 6);

Posted: Wed Sep 05, 2007 9:43 am
by CoderGoblin
I hate it when people know more php functions than I do..:lol:. Good catch stereofrog, far easier.

Posted: Wed Sep 05, 2007 2:10 pm
by MikeCXT
Before I title the thread [Solved], I have a quick question for future clarification.

Both CoderGoblin's and stereofrog's ways worked perfect. I actually like CoderGoblins due to repetition being allowed. Stereofrog's has no repetition, but thats just my person preference for my coupon codes... Thanks for all for this help.

Now, using CoderGoblin's code, he uses

Code: Select all

$coupon.=$convert_array[rand (0, $max_choice)];
What exactly does that period after the variable "coupon" do? Does it allow you to just append the $convert_array code to it with each repetition of the for statement? And if so, it just adds to the end of the string that is the variable? I've seen that before, but never really understood it, and never ran across an explaination in my php books. Thanks.

Posted: Wed Sep 05, 2007 2:16 pm
by John Cartwright
MikeCXT wrote:And if so, it just adds to the end of the string that is the variable?
Exactly. :)