redarrow Posted January 6, 2008 Share Posted January 6, 2008 $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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/84772-solved-shuffle-wont-work-correctly-with-a-caternation/ Share on other sites More sharing options...
GingerRobot Posted January 6, 2008 Share Posted January 6, 2008 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/84772-solved-shuffle-wont-work-correctly-with-a-caternation/#findComment-432020 Share on other sites More sharing options...
redarrow Posted January 6, 2008 Author Share Posted January 6, 2008 thanks GingerRobot enjoying your demostration mate....... Quote Link to comment https://forums.phpfreaks.com/topic/84772-solved-shuffle-wont-work-correctly-with-a-caternation/#findComment-432028 Share on other sites More sharing options...
redarrow Posted January 6, 2008 Author Share Posted January 6, 2008 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......... Quote Link to comment https://forums.phpfreaks.com/topic/84772-solved-shuffle-wont-work-correctly-with-a-caternation/#findComment-432031 Share on other sites More sharing options...
Ken2k7 Posted January 6, 2008 Share Posted January 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/84772-solved-shuffle-wont-work-correctly-with-a-caternation/#findComment-432032 Share on other sites More sharing options...
Barand Posted January 6, 2008 Share Posted January 6, 2008 if you have PHP5, you could use your original code with $h=str_split($i); replacing your $h=explode(' ',$i); Quote Link to comment https://forums.phpfreaks.com/topic/84772-solved-shuffle-wont-work-correctly-with-a-caternation/#findComment-432040 Share on other sites More sharing options...
GingerRobot Posted January 6, 2008 Share Posted January 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/84772-solved-shuffle-wont-work-correctly-with-a-caternation/#findComment-432050 Share on other sites More sharing options...
redarrow Posted January 6, 2008 Author Share Posted January 6, 2008 thank you all solved Quote Link to comment https://forums.phpfreaks.com/topic/84772-solved-shuffle-wont-work-correctly-with-a-caternation/#findComment-432063 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.