Php 数组键为随机数,重复项为相同的随机数
我试图通过为每个任务生成从1到2的随机数来为两个人随机分割任务 问题是我有重复的任务。 如何在不删除副本的情况下为副本生成相同的编号? 例如:Php 数组键为随机数,重复项为相同的随机数,php,Php,我试图通过为每个任务生成从1到2的随机数来为两个人随机分割任务 问题是我有重复的任务。 如何在不删除副本的情况下为副本生成相同的编号? 例如: <?php $tasks = array('task1','task4','task2','task3','task4','task3','task4','task5'); foreach($tasks as $task) { $rand = rand(1,2); $array = array('task' => $t
<?php
$tasks = array('task1','task4','task2','task3','task4','task3','task4','task5');
foreach($tasks as $task)
{
$rand = rand(1,2);
$array = array('task' => $task,'rand'=> $rand);
echo $array['task']."==>";
echo $array['rand']."<br>";
}
?>
简单方法:使用数组键
结果:var\u dump($output)代码>
获取特定任务的编号:
var_dump($output['task3'])代码>
这很容易使用,但重复项将被删除。正如你所说,你不喜欢那样。(但这会更容易)
不删除并保留原始数组排序
在这里,您需要一个新的数组,带有任务和编号
$tasks = array('task1', 'task4', 'task2', 'task3', 'task4', 'task3', 'task4', 'task5');
$numbers = []; // saving the random number, so the same task will get the same number
$result = [];
foreach ($tasks as $task) {
if (isset($result[$task])) {
$number = $numbers[$task];
} else {
$number = rand(1, 2);
$numbers[$task] = $number;
}
$result[] = [
'task' => $task,
'number' => $number,
];
// or $result[] = $number;
// then only the number is saved at the position of $task
}
结果<代码>变量转储($result)代码>
简单的方法:使用数组键
结果:var\u dump($output)代码>
获取特定任务的编号:
var_dump($output['task3'])代码>
这很容易使用,但重复项将被删除。正如你所说,你不喜欢那样。(但这会更容易)
不删除并保留原始数组排序
在这里,您需要一个新的数组,带有任务和编号
$tasks = array('task1', 'task4', 'task2', 'task3', 'task4', 'task3', 'task4', 'task5');
$numbers = []; // saving the random number, so the same task will get the same number
$result = [];
foreach ($tasks as $task) {
if (isset($result[$task])) {
$number = $numbers[$task];
} else {
$number = rand(1, 2);
$numbers[$task] = $number;
}
$result[] = [
'task' => $task,
'number' => $number,
];
// or $result[] = $number;
// then only the number is saved at the position of $task
}
结果<代码>变量转储($result)代码>
您可以使用array\u fill\u keys()
创建一个数组,将所有任务作为键,然后使用array\u walk()
为所有键分配随机数
$tasks = array('task1','task4','task2','task3','task4','task3','task4','task5');
//use the tasks as keys for a new array. Duplicates will be filtered out.
$task_rand = array_fill_keys( $tasks, false );
//then "walk" the new array, assigning a randum number to each task
array_walk($task_rand, function(&$value){$value= rand(1,2);});
//sort the array, so task1 comes before task2 etc
ksort($task_rand);
The result of `$task_rand`:
Array
(
[task1] => 2
[task2] => 1
[task3] => 2
[task4] => 1
[task5] => 1
)
您可以使用array\u fill\u keys()
创建一个数组,将所有任务作为键,然后使用array\u walk()
为所有键分配随机数
$tasks = array('task1','task4','task2','task3','task4','task3','task4','task5');
//use the tasks as keys for a new array. Duplicates will be filtered out.
$task_rand = array_fill_keys( $tasks, false );
//then "walk" the new array, assigning a randum number to each task
array_walk($task_rand, function(&$value){$value= rand(1,2);});
//sort the array, so task1 comes before task2 etc
ksort($task_rand);
The result of `$task_rand`:
Array
(
[task1] => 2
[task2] => 1
[task3] => 2
[task4] => 1
[task5] => 1
)
你能举个例子说明你想要什么样的输出吗?你能举个例子说明你想要什么样的输出吗?在你的第二段代码中,你有一个数组$result
,它从未以$result[$task]
有意义的方式添加到数组中代码>在那里,当前条目被添加到$result
对不起-我在完成评论之前按了回车键。啊,好的。但是,如果第二次使用数组键,则只需更改已有的条目。你不会再加什么了。这意味着,重复项将被删除。见解决方案1。这种情况就是这样发生的。但目前,此代码可能会为同一个任务返回不同的数字,因为它永远无法将任务与上一个选择的任务相匹配。在第二段代码中,您有一个数组$result
,该数组从未以$result[$task]
有意义的方式添加到其中。请查看$result[]=['task'=>$task,'number'=>$number,]
在那里,当前条目被添加到$result
抱歉-我在完成我的评论之前按了enter键。啊,好的。但是如果你第二次使用数组键,你只会更改你已有的条目。你不会添加任何内容。这意味着,重复的条目将被删除。请参阅解决方案1。这样会发生这种情况。但目前,这段代码可能会为同一个任务返回不同的数字,因为它永远无法将一个任务与之前选择的任务进行匹配。
array (size=8)
0 =>
array (size=2)
'task' => string 'task1' (length=5)
'number' => int 2
1 =>
array (size=2)
'task' => string 'task4' (length=5)
'number' => int 1
2 =>
array (size=2)
'task' => string 'task2' (length=5)
'number' => int 2
3 =>
array (size=2)
'task' => string 'task3' (length=5)
'number' => int 2
4 =>
array (size=2)
'task' => string 'task4' (length=5)
'number' => int 1
5 =>
array (size=2)
'task' => string 'task3' (length=5)
'number' => int 2
6 =>
array (size=2)
'task' => string 'task4' (length=5)
'number' => int 1
7 =>
array (size=2)
'task' => string 'task5' (length=5)
'number' => int 1
$tasks = array('task1','task4','task2','task3','task4','task3','task4','task5');
//use the tasks as keys for a new array. Duplicates will be filtered out.
$task_rand = array_fill_keys( $tasks, false );
//then "walk" the new array, assigning a randum number to each task
array_walk($task_rand, function(&$value){$value= rand(1,2);});
//sort the array, so task1 comes before task2 etc
ksort($task_rand);
The result of `$task_rand`:
Array
(
[task1] => 2
[task2] => 1
[task3] => 2
[task4] => 1
[task5] => 1
)