redarrow Posted April 20, 2008 Share Posted April 20, 2008 Tried to shuffle the array using array_rand and shulle but wrong output...... So how can i shuflle the array to keep it's syntex in correct concept of the current script......... I want to be able to shuffle all the urls and keep the order and show there order in a shuffled output but work as codeded....... <?php $i=array( "Rescipe 1"=>"".$_SERVER['PHP_SELF']."?cmd=rescipe1" ,"Rescipe 2"=>"".$_SERVER['PHP_SELF']."?cmd=rescipe2" ,"Rescipe 3"=>"".$_SERVER['PHP_SELF']."?cmd=rescipe3" ,"Rescipe 4"=>"".$_SERVER['PHP_SELF']."?cmd=rescipe4" ,"Rescipe 5"=>" ".$_SERVER['PHP_SELF']."?cmd=rescipe5"); foreach($i as $key => $val){ echo "link $key <br> <a href='$val'>$key</a><br><br>"; } switch ($_GET['cmd']){ case rescipe1: echo "this is rescipe 1"; break; case rescipe2: echo "this is rescipe 2"; break; case rescipe3: echo "this is rescipe 3"; break; case rescipe4: echo "this is rescipe 4"; break; case rescipe5: echo "this is rescipe 5"; break; } ?> Link to comment https://forums.phpfreaks.com/topic/101995-solved-shuffle-array/ Share on other sites More sharing options...
dptr1988 Posted April 20, 2008 Share Posted April 20, 2008 Edit reason: To delete my reply. I guess didn't read to original message carefully enough Link to comment https://forums.phpfreaks.com/topic/101995-solved-shuffle-array/#findComment-521985 Share on other sites More sharing options...
redarrow Posted April 20, 2008 Author Share Posted April 20, 2008 so how do i add that to the current code trying m8 and thank you......... Link to comment https://forums.phpfreaks.com/topic/101995-solved-shuffle-array/#findComment-521991 Share on other sites More sharing options...
wildteen88 Posted April 20, 2008 Share Posted April 20, 2008 Try: <?php /* FUNCTION TAKEN FROM PHP.NET LINK: http://uk.php.net/manual/en/function.shuffle.php#80586 */ function shuffle_assoc(&$array) { //$keys needs to be an array, no need to shuffle 1 item anyway if (count($array)>1) { $keys = array_rand($array, count($array)); foreach($keys as $key) $new[$key] = $array[$key]; $array = $new; } //because it's a wannabe shuffle(), which returns true return true; } $recipies = array( 'Rescipe 1' => $_SERVER['PHP_SELF'].'?cmd=rescipe1', 'Rescipe 2' => $_SERVER['PHP_SELF'].'?cmd=rescipe2', 'Rescipe 3' => $_SERVER['PHP_SELF'].'?cmd=rescipe3', 'Rescipe 4' => $_SERVER['PHP_SELF'].'?cmd=rescipe4', 'Rescipe 5' => $_SERVER['PHP_SELF'].'?cmd=rescipe5' ); shuffle_assoc($recipies); foreach($recipies as $key => $value) { echo "link $key <br> <a href=\"$value\">$key</a><br><br>"; } if(isset($_GET['cmd']) && !empty($_GET['cmd'])) { switch ($_GET['cmd']) { case 'rescipe1': echo "this is rescipe 1"; break; case 'rescipe2': echo "this is rescipe 2"; break; case 'rescipe3': echo "this is rescipe 3"; break; case 'rescipe4': echo "this is rescipe 4"; break; case 'rescipe5': echo "this is rescipe 5"; break; } } ?> Link to comment https://forums.phpfreaks.com/topic/101995-solved-shuffle-array/#findComment-521997 Share on other sites More sharing options...
redarrow Posted April 20, 2008 Author Share Posted April 20, 2008 Thank you wildteen 10/10 m8............. solved Link to comment https://forums.phpfreaks.com/topic/101995-solved-shuffle-array/#findComment-521999 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.