简体   繁体   English

2个具有不同值的不同数组

[英]2 different array with different values

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. 我实际上是在尝试使mysql的阵列与我已经shuffleshuffle的阵列不同。 I want to maintain the array keys, just the value in different order. 我想维护数组键,只是保持值的顺序不同。

Here is the example, 这是例子

Array from MYSQL: 来自MYSQL的数组:

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

Array after shuffle : shuffleshuffle

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. 如果您注意到, MYSQL array键3与shuffle array具有相同的值,我希望它有所不同。 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);

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);

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM