Jump to content

[SOLVED] shuffle wont work correctly with a caternation


redarrow

Recommended Posts

$i varable $s.$p should shuffle with mixed letters and numbers why not please.......

 

 

the caternated varable $i wont shuffle with example 2j64 it always dsf345

letter then number............

 

must be the caternation but why please........

<?php

$a=range(a,z);
shuffle($a);
$c=implode('',$a);
$s=substr("$c",0,3);

$z=range(0,9);
shuffle($z);
$d=implode('',$z);
$p=substr("$d",0,3);

$i=$s.$p;
$h=explode(' ',$i);
shuffle($h);
$z=implode(' ',$h);
echo $z;

?>

Link to comment
Share on other sites

The problem is with this line:

 

$h=explode(' ',$i);

 

The characters and letters aren't separated by spaces, so there's nothing to explode by. explode() therefore returns an array with one element - the two strings joined together.

 

This is pretty similar to what you were trying to do:

 

<?php
$nums = range(0,9);
$nums_used = array_rand($nums,3);
$chars = range('a','z');
$chars = array_flip($chars);//array rand returns keys, not values - we want the values, so flip this array.
$chars_used = array_rand($chars,3);
$array = array_merge($nums_used,$chars_used);
shuffle($array);
$z = implode('',$array);
echo $z;
?>

Link to comment
Share on other sites

Hi there how comes you only use array_flip once but use array_rand twice is it becouse your merging the arrays together you only need the array_flip used once.........

Once for randomizing between the numbers 0-9 and selecting 3 numbers and then another to chars used array. Also you don't array_flip on an array of numbers :S

 

Also it's hard to index an array with letters using it's letters with the array of numbers given in the first array.

Link to comment
Share on other sites

Hi there how comes you only use array_flip once but use array_rand twice is it becouse your merging the arrays together you only need the array_flip used once.........

 

The array_rand() function returns the keys of the array, but we want the values. However, in the array of the numbers, the keys are identical to the values, so there's no need to use array_flip() on that array.

 

if you have PHP5, you could use your original code with

 

$h=str_split($i);

 

replacing your

 

$h=explode(' ',$i);

 

I was looking for that function.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.