重新排列数组,使PHP中的相邻值不相同
我需要在两个并发值不相同的情况下以随机顺序洗牌数组重新排列数组,使PHP中的相邻值不相同,php,arrays,Php,Arrays,我需要在两个并发值不相同的情况下以随机顺序洗牌数组 $array("red", "green", "blue", "red", "green", "blue", "red", "green", "blue", etc); 输出应该是任意有效数组,该数组以随机顺序包含所有值,且两个值不相邻 因此,有效的选择是: $array("blue", "red", "blue", "green", "red", "blue", "green"); 我想数一数“红”、“蓝”和“绿”的数量 然后可能使用每个
$array("red", "green", "blue", "red", "green", "blue", "red", "green", "blue", etc);
输出应该是任意有效数组,该数组以随机顺序包含所有值,且两个值不相邻
因此,有效的选择是:
$array("blue", "red", "blue", "green", "red", "blue", "green");
我想数一数“红”、“蓝”和“绿”的数量
然后可能使用每个字符串的可用数量创建一个数组。这里是我的第一个方法,旨在最小化循环以获得所需的结果 代码:()
//在此处添加$array
$length=sizeof($array);
洗牌($数组);
$valcounts=array\u count\u值($array);
函数连续检查($array){
$loops=sizeof($array)-1;//最后一个元素将没有用于比较的右侧元素
对于($i=0;$i3){//如果两个元素之间有3个颜色不匹配的数组_拼接($array,$prevj+2,0,数组_拼接($array,$colorkeys[$i],1));
打破
}
$prevj=$colorkeys[$j];
}
}
$colorkeys=array_keys($array,$color);//更新颜色键数组以继续处理
}
$prevk=$colorkeys[$i];
}
}
}
var_导出($array);//有效的
}否则{
回显“\n\n”;
}
这是我的第二个方法,它使用正则表达式模式
代码:()
shuffle($array);
$string=内爆(“”,$array);
$start_length=strlen($string);
foreach(数组\唯一($array)作为$v){
$pullcount=$pushcount1=$pushcount2=0;
$string=preg_replace(“/$v(?=$v)/”,“”,$string,-1,$pullcount);//删除每个冲突对的第一个值
$string=preg_replace(“/\K(?对于数组中的数据计数,您知道它是否总是可以求解的,例如起点永远不会是(红色、红色、红色、蓝色)?数组将始终是一个按顺序排列的红色、绿色、蓝色列表,以其中任何一个为结尾。@mickmackusa我需要它用于php,所以我在这里问它。@HugoCox抱歉,我没有注意到链接问题的标记。已删除标记。@HugoCox等等,它们的顺序已经满足您的条件了吗?您只想在保持否的同时随机化它ide附带条件?非常感谢,这将帮助我进一步了解:)
array_count_values($array);
array_count_values($array)["red"];
array_count_values($array)["green"];
array_count_values($array)["blue"];
// add $array here
$length=sizeof($array);
shuffle($array);
$valcounts=array_count_values($array);
function consec_check($array){
$loops=sizeof($array)-1; // last element will not have right side element for comparison
for($i=0; $i<$loops; ++$i){
if($array[$i]==$array[$i+1]){
return false; // consecutive equal values = invalid
}
}
return true;
}
if(max($valcounts)<=ceil($length/2)){ // if logically possible to fix
while(!consec_check($array)){ // while any two equal elements are consecutive
foreach(array_diff($valcounts,[1]) as $color=>$count){ // only bother with elements that occur more than once
$colorkeys=array_keys($array,$color); // color group keys
for($i=0; $i<$count; ++$i){
if($i>0 && $prevk+1==$colorkeys[$i]){ // identify consecutives elements with same color
if($colorkeys[0]!=0){ // safe to shift {$colorkeys[$i]} to first position
array_unshift($array,array_splice($array,$colorkeys[$i],1)[0]);
}elseif(end($colorkeys)!=$length-1){ // safe to push {$colorkeys[$i]} to the last position
array_push($array,array_splice($array,$colorkeys[$i],1)[0]);
}else{ // no easy option, find a safe location inside array (more frequently used as array length increases)
for($j=0; $j<$count; ++$j){
if($j>0 && $colorkeys[$j]-$prevj>3){ // if 3 off-colors between two elements array_splice($array,$prevj+2,0,array_splice($array,$colorkeys[$i],1));
break;
}
$prevj=$colorkeys[$j];
}
}
$colorkeys=array_keys($array,$color); // update color keys array for continued processing
}
$prevk=$colorkeys[$i];
}
}
}
var_export($array); // valid
}else{
echo "\n\n<a href=\"https://www.youtube.com/watch?v=XAYhNHhxN0A\">Array cannot be made valid.</a>";
}
shuffle($array);
$string=implode(' ',$array);
$start_length=strlen($string);
foreach(array_unique($array) as $v){
$pullcount=$pushcount1=$pushcount2=0;
$string=preg_replace("/$v (?=$v)/","",$string,-1,$pullcount); // remove the first value of each conflicting pair
$string=preg_replace("/ \K(?<!$v )(?!$v)|^(?!$v)/","$v ",$string,$pullcount,$pushcount1); // foreach removal, re-insert value(s) where valid
if($pullcount<=$pushcount1){
$string=preg_replace("/$(?<!$v)/"," $v",$string,$pullcount-$pushcount1,$pushcount2);
}
if($pullcount!=$pushcount1+$pushcount2){
echo "failure while replacing $v $pullcount & ",$pushcount1+$pushcount2,"\n";
break;
}else{
echo "successfully replaced $pullcount conflicts for $v\n";
}
}
if($start_length==strlen($string)){
$array=explode(" ",$string);
var_export($array);
}else{
echo "\n<a href=\"https://www.youtube.com/watch?v=XAYhNHhxN0A\">Array cannot be made valid.</a>";
}