Array Imploding erasing duplicates...?[SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MikeCXT
Forum Newbie
Posts: 14
Joined: Fri Jan 13, 2006 1:26 pm

Array Imploding erasing duplicates...?[SOLVED]

Post 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-
Last edited by MikeCXT on Wed Sep 05, 2007 2:38 pm, edited 1 time in total.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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
Last edited by CoderGoblin on Wed Sep 05, 2007 9:00 am, edited 1 time in total.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Array Imploding erasing duplicates...?

Post by superdezign »

Can't you just use a string instead of an array?

EDIT: CoderGoblin's code does just that.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

A little bug fix for CoderGoblin's code :)
It should be:

Code: Select all

$coupon.=$convert_array[rand (0, $max_choice)];
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Oops... :oops:
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

Another approach

Code: Select all

$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$coupon = substr(str_shuffle($chars), 0, 6);
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

I hate it when people know more php functions than I do..:lol:. Good catch stereofrog, far easier.
MikeCXT
Forum Newbie
Posts: 14
Joined: Fri Jan 13, 2006 1:26 pm

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

MikeCXT wrote:And if so, it just adds to the end of the string that is the variable?
Exactly. :)
Post Reply