简体   繁体   English

确保 php 中相同的数组项不相邻

[英]Make sure the same array items are not next to each other in php

I am struggling with this case.我正在为这个案子而苦苦挣扎。 I have a random array that similar items can be in the next to each other and I don't want it to happen.我有一个随机数组,类似的项目可以彼此相邻,我不希望它发生。

Example:例子:

array("red","red","blue","green","green","blue");

Expected Output : array("red","blue","red","green","blue","green")

So I was thinking about shuffle the items until there are no similar items in the next to each other.所以我正在考虑将这些项目洗牌,直到彼此相邻的项目中没有类似的项目。 But it will take a lot of time if the array has a lot of values.但是如果数组有很多值,会花费很多时间。

Is there any effective way to make it happen?有什么有效的方法可以实现吗?

Any help would be appreciated.任何帮助,将不胜感激。 Thanks谢谢

Just an idea:只是一个想法:

  • Shuffle the original array.随机播放原始数组。
  • Compare the first two.比较前两个。 If they are the same, swap second with the last.如果它们相同,则将第二个与最后一个交换。
  • Move to the next pair.移动到下一对。
  • Repeat until end reached.重复直到结束。

Here we assume the worst case shuffle having the same values next together.在这里,我们假设最坏的情况下洗牌具有相同的值。

$colors = ["red", "red", "blue", "blue", "green", "green"];

for ($i = 0; $i < count($colors) - 1; $i++) {
    if ($colors[$i] !== $colors[$i + 1]) continue;
    $last = $colors[count($colors) - 1];
    $colors[count($colors) - 1] = $colors[$i];
    $colors[$i + 1] = $last;
}

print_r($colors);

Array
(
    [0] => red
    [1] => green
    [2] => blue
    [3] => red
    [4] => green
    [5] => blue
)

I ended up re-arrange to a new array.我最终重新安排了一个新的阵列。 For those who have the same problem with me here is how I do it:对于那些和我有同样问题的人,我是这样做的:

<?php

$colors = ["red","red","red","red","red","red","blue","blue","green","green","green","green","green","green","red","red","red"];
$newColors = array();

$uniqColors = array_values(array_unique($colors));

$countUniqColors = count($uniqColors);

$y = 0;

for ($i = 0; $i <= count($colors) - 1; $i++) {
    $newColors[$i] = $uniqColors[$y];
    $y += 1;
    if ($y >= $countUniqColors) $y = 0; 
}

print_r($newColors);

?>

I think this is the most effective way I can think of.我认为这是我能想到的最有效的方法。 Let me know if you have any thought about this solution如果您对此解决方案有任何想法,请告诉我

Just another idea:只是另一个想法:

  • Take the array and scan from start to end取数组,从头到尾扫描
  • Compare the next with current, when not equal, then continue比较下一个和当前,当不相等时,然后继续
  • Compare the next with current, when equal, search the next different and swap next with next different将下一个与当前进行比较,当相等时,搜索下一个不同并将下一个与下一个不同交换

Based on your comment of an even more worse case than in the original question, I suggest trying this, ensuring never one word will be twice next together.根据您对比原始问题更糟糕的情况的评论,我建议尝试这样做,确保永远不会有一个单词会出现两次。

$colors = ["red", "red", "red", "red", "red", "red", "blue", "blue", "green", "green", "green", "green", "green", "green", "red", "red", "red"];

for ($i = 0; $i < count($colors) - 1; $i++) {
    if ($colors[$i] !== $colors[$i + 1]) continue;

    for ($k = $i + 2; $k < count($colors) - 1; $k++) {
        if ($colors[$i + 1] === $colors[$k]) continue;
        $tmp = $colors[$i + 1];
        $colors[$i + 1] = $colors[$k];
        $colors[$k] = $tmp;
        break;
    }
}

print_r($colors);

Array
(
    [0] => red
    [1] => blue
    [2] => red
    [3] => blue
    [4] => red
    [5] => green
    [6] => red
    [7] => green
    [8] => red
    [9] => green
    [10] => red
    [11] => green
    [12] => red
    [13] => green
    [14] => red
    [15] => green
    [16] => red
)

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

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