Methods in PHP to control the order of appearance of array elements based on probability: To disrupt the order of the array: use the shuffle() function. Assign probabilities: Use array_map() to assign a probability (0-1) to each element. Sort weighted array: Sort the array in descending order of probability (elements with higher probability first). Extract elements: Extract elements from the sorted array in sequence, that is, scramble the array in a probability-controlled order.
Using probability control in PHP to scramble the order of appearance of array elements
Disrupting the order of elements in an array is a common problem in many development scenarios Common requirements, such as random draws, game mechanisms, etc. If you need to control the order of appearance of elements according to probability after shuffling the array, PHP provides appropriate functions and methods.
Preparation
First, prepare an array containing the elements to be scrambled. For example:
$array = [1, 2, 3, 4, 5];
Use the shuffle()
function to shuffle the array
shuffle()
The function can shuffle the array The order of the elements.
shuffle($array);
Use probability to control the order in which elements appear
Use the array_map()
method to traverse the array elements and assign a probability to each element. The probability value ranges from 0 to 1, where 0 means that the element will never appear and 1 means that the element will always appear.
$probabilities = [0.2, 0.3, 0.4, 0.5, 0.6]; $weightedArray = array_map(function ($element, $probability) { return [$element, $probability]; }, $array, $probabilities);
Sort weighted array
Sort the weighted array, the element with higher probability will be at the beginning of the array.
usort($weightedArray, function ($a, $b) { return $b[1] <=> $a[1]; });
Extract elements
Now, extract elements from the sorted weighted array in sequence to obtain a scrambled array in a probability-controlled order.
$reorderedArray = []; foreach ($weightedArray as $element) { $reorderedArray[] = $element[0]; }
Practical Case
Suppose there is a game that requires randomly selecting 3 items from an array containing 5 items. The probability of each item appearing is:
You can follow the above steps to implement the following code:
$items = ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5']; $probabilities = [0.2, 0.3, 0.4, 0.5, 0.6]; shuffle($items); $weightedItems = array_map(function ($item, $probability) { return [$item, $probability]; }, $items, $probabilities); usort($weightedItems, function ($a, $b) { return $b[1] <=> $a[1]; }); $drawnItems = []; for ($i = 0; $i < 3; $i++) { $drawnItems[] = $weightedItems[$i][0]; } print_r($drawnItems);
Run the above code and control the output according to probability 3 items drawn sequentially.
The above is the detailed content of How to control the order of appearance of elements through probability after the PHP array is shuffled?. For more information, please follow other related articles on the PHP Chinese website!