0

My question may sound abit silly, but I know it is possible to do. I'm actually trying to make the array from mysql to be different from the array I have shuffle it already. I want to maintain the array keys, just the value in different order.

Here is the example,

Array from MYSQL:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
)

Array after shuffle:

Array
(
    [0] => 3
    [1] => 7
    [2] => 8
    [3] => 4
    [4] => 1
    [5] => 2
    [6] => 5
    [7] => 6
)

If you notice, MYSQL array key 3 has the same value as shuffle array, I want it to be different. How can I do so?

Here is my code:

function get_random_elements($array) {
    $ori_array = $array;

    echo "<pre>";
    print_r($ori_array);
    echo "</pre>";

    shuffle($array);

    echo "<pre>";
    print_r($array);
    echo "</pre>";

    for($x=0; $x<count($array); $x++) {
        if ($array[$x] == $ori_array[$x])
        {
            $dupliarray[] = "Array value: ".$array[$x]." Key :".$x;
            unset($array[$x]);
        }

    }

    echo "<pre>";
    print_r($dupliarray);
    echo "</pre>";

}

$mysql_array = array(0=>'1',1=>'2',2=>'3',3=>'4',4=>'5',5=>'6',6=>'7',7=>'8');

get_random_elements($mysql_array);
2
  • call get_random_elements function recursively until(which is count($dupliarray) is 0) you get totally different array from the original array. Commented Nov 30, 2016 at 9:36
  • @Naga Hmm... true, but how do I call recursively? Commented Nov 30, 2016 at 9:47

2 Answers 2

2

One solution can be to shuffle array until it became totally differ from source array:

$sourceArray = [0, 1, 2, 3, 4, 5, 6, 7, 8];
$shuffledArray = $sourceArray; //because function shuffle will change passed array

shuffle($shuffledArray);    
while (count(array_intersect_assoc($sourceArray, $shuffledArray))) {
    shuffle($shuffledArray);
}

echo '<pre>';
var_dump($sourceArray, $shuffledArray);
Sign up to request clarification or add additional context in comments.

Comments

0

I suggest this variant: Code in sandbox

$startArray = [
    0 => 1,
    1 => 2,
    2 => 3,
    3 => 4,
    4 => 5,
    5 => 6,
    6 => 7,
    ];

function getShuffledArray($startArray) {

    $shuffledArray = [];

    // save value of last element for situation when we have last 2 elements of start array and one of them - last
    $lastElement = end($startArray);

    while (count($startArray)) {
        $candidateIndex = array_rand($startArray);
        if ($candidateIndex == count($shuffledArray)) {
            while ($candidateIndex == count($shuffledArray)) {
                $candidateIndex = array_rand($startArray);
            }
        }
        $shuffledArray[] = $startArray[$candidateIndex];
        unset($startArray[$candidateIndex]);

        // shuffle last two elements when one of them last (in start array)
        if (count($startArray) == 2 && end($startArray) == $lastElement) {
            $shuffledArray[] = end($startArray);
            $shuffledArray[] = reset($startArray);
            $startArray = [];
        }
    }

    return $shuffledArray;
}

$shuffledArray = getShuffledArray($startArray);
print_r ($shuffledArray);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.