Jump to content

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

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.