Jump to content

[SOLVED] shuffle array..........


redarrow

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.