Hello. There is, for example, such an array:

<?php $arr_assoc["Галина"] = "Петро"; $arr_assoc["Катерина"] = "Роман"; $arr_assoc["Дарина"] = "Микола"; $arr_assoc["Олена"] = "Александр"; $arr_assoc["Мария"] = "Андрей"; ?> 

I need to do so that the array is mixed up, the names of the girls remain in place, and the guys change, and so if the guy gets to the same girl that was before, the process started again until everyone has a new guy?

  • 3
    Funny sex toy work ...) - Indifferent
  • No, you thought wrong - this is for my dating site. - Csharp
  • Are all the guys names unique? - nitrocaster
  • Yes, unique. - Csharp

3 answers 3

Invented such a crutch:

  $arr_assoc = array(); $arr_assoc["Галина"] = "Петро"; $arr_assoc["Катерина"] = "Роман"; $arr_assoc["Дарина"] = "Микола"; $arr_assoc["Олена"] = "Александр"; $arr_assoc["Мария"] = "Андрей"; $m = array_values($arr_assoc); $w = array_keys($arr_assoc); $new_assoc = array(); do { shuffle($m); shuffle($w); $new_assoc = array_combine($w, $m); } while (count(array_intersect_assoc($new_assoc, $arr_assoc)) != 0); print_r($new_assoc); /* Результат Array ( [Дарина] => Петро [Мария] => Роман [Олена] => Микола [Катерина] => Александр [Галина] => Андрей ) */ 

But I would not use it - you should reconsider the algorithm.

  • Just like playing stone-scissors-paper with ten ;-) - lampa
  • Take a list of all permutations, exclude those having intersections with the original array and randomly select the result. But, since we are talking about php, this option seems delusional. A little more pairs - and the result can not be expected. (By the way, checking for intersection is trivial, if you rearrange the indices from 1 to n or from 0 to n-1: see if there are i values ​​in place i) - alexlz
  • Your algorithm is not really as bad as it seems: it is short, transparent and easy to understand. But unnecessarily shuffle both keys and values, do one thing. The chances that the cycle will work 2 times: 1 out of 6, which is 3 times: 1 out of 36, and so on. In our time, such a loss of performance is not significant. - ReinRaus
 $arr_assoc["Галина"] = "Петро"; $arr_assoc["Катерина"] = "Роман"; $arr_assoc["Дарина"] = "Микола"; $arr_assoc["Олена"] = "Александр"; $arr_assoc["Мария"] = "Андрей"; $m = array_values($arr_assoc); $w = array_keys($arr_assoc); $size = sizeof($w); for($i=0; $i < $size; $i++) { $new_assoc[$w[$i]] = $m[($size == $i+1) ? 0 : ($i+1)]; } var_dump($new_assoc); 
  • There is no random. Normal sequential gangbang. - Indifferent
  • @ Indifferent - so the author did not need to randomly interfere, because he didn’t specify how to :)) lampa

Here is my attempt:

 $arr_assoc["Галина"] = "Петро"; $arr_assoc["Катерина"] = "Роман"; $arr_assoc["Дарина"] = "Микола"; $arr_assoc["Олена"] = "Александр"; $arr_assoc["Мария"] = "Андрей"; echo var_dump($arr_assoc); $m = array_values($arr_assoc); $w = array_keys($arr_assoc); while(true) { $res = array(); $m1 = $m; for($i=0; $i < count($w) - 1; $i++) { $herboy = $arr_assoc[$w[$i]]; $herboy1 = ""; /* убираем её боя, если его кто-то ещё не увёл */ /* лучше заменить на array_search */ for($j = 0; $j < count($m1); $j++) if ($m1[$j] == $herboy) { unset($m1[$j]); $m1 = array_values($m1); $herboy1 = $herboy; break; } $k = rand(0, count($m1) - 1); $res[$w[$i]] = $m1[$k]; unset($m1[$k]); if($herboy1 != "") array_push($m1, $herboy1); $m1 = array_values($m1); } /* Не остался ли последней девушке надоевший ей бойфренд */ if($arr_assoc[$w[$i]] != $m1[0]) { $res[$w[$i]] = $m1[0]; break; } } echo var_dump($res);