php 数组洗牌,以相同的方式洗牌2個php陣列

本文探讨了如何在保持两个数组元素之间特定关联的前提下,对它们进行随机打乱。提供了六种方法,包括合并数组、使用临时数组、利用排序和数组多维操作,适用于不同场景,如对象、数组类型,并考虑了重复值和数组长度一致的情况。
摘要由CSDN通过智能技术生成

I have this for example:

我有這個例子:

$array['one'][0] = 0;

$array['one'][1] = 1;

$array['one'][2] = 2;

$array['one'][3] = 3;

$array['two'][0] = 00;

$array['two'][1] = 11;

$array['two'][2] = 22;

$array['two'][3] = 33;

How can I shuffle them both to get something like:

我怎樣才能將它們混合起來以獲得類似的東西:

$array['one'][0] = 2;

$array['one'][1] = 1;

$array['one'][2] = 3;

$array['one'][3] = 0;

$array['two'][0] = 22;

$array['two'][1] = 11;

$array['two'][2] = 33;

$array['two'][3] = 00;

Or any other random order, but having the same "random factor" in both?

或者任何其他隨機順序,但兩者中都有相同的“隨機因素”?

For example, I want that $array['one'][0] and $array['two'][0] get shuffled to get $array['one'][x] and $array['two'][x] (x being a random key, but the SAME on both arrays).

例如,我希望$ array ['one'] [0]和$ array ['two'] [0]得到洗牌以獲得$ array ['one'] [x]和$ array ['two'] [ x](x是隨機密鑰,但兩個陣列上的SAME)。

6 个解决方案

#1

3

Something like this could work. It's similar to Mimikry's answer except this one will work even if you happen to have duplicate values in array one (this one doesn't use values of array one as keys of the temporary array).

像這樣的東西可以工作。它類似於Mimikry的答案,除非你碰巧在數組1中有重復的值(這一個不使用數組1的值作為臨時數組的鍵),否則它將起作用。

Assuming both arrays are of the same size.

假設兩個陣列的大小相同。

$c = count($array['one']);

$tmp = array();

for ($i=0; $i

$tmp[$i] = array($array['one'][$i], $array['two'][$i]);

}

shuffle($tmp);

for ($i=0; $i

$array['one'][$i] = $tmp[$i][0];

$array['two'][$i] = $tmp[$i][1];

}

#2

17

$count = count($array['one']);

$order = range(1, $count);

shuffle($order);

array_multisort($order, $array['one'], $array['two']);

Works with arrays with elements of any type (objects and arrays too).

適用於具有任何類型元素的數組(對象和數組)。

This way may by used with any number of arrays (not only two).

這種方式可以與任意數量的數組(不僅僅是兩個)一起使用。

Works with duplicated values.

使用重復的值。

Clean code.

清潔代碼。

#3

0

hopefully i get you right.

希望我能幫到你。

For your example above, this code should work. But it's pretty hacky...

對於上面的示例,此代碼應該有效。但它非常hacky ......

$array['one'][0] = 'A';

$array['one'][1] = 'B';

$array['one'][2] = 'C';

$array['one'][3] = 'D';

$array['two'][0] = 'AA';

$array['two'][1] = 'BB';

$array['two'][2] = 'CC';

$array['two'][3] = 'DD';

// save the dependencies in tmp array

foreach ($array['two'] as $key => $val) {

$tmp[$array['one'][$key]] = $val;

}

shuffle($array['one']);

// restore dependencies in tmp2 array

foreach ($array['one'] as $key => $val) {

$tmp2[$key] = $tmp[$val];

}

// overwrite with restore array

$array['two'] = $tmp2;

var_dump($array):

的var_dump($陣列):

array(2) {

["one"]=>

array(4) {

[0]=>

string(1) "B"

[1]=>

string(1) "A"

[2]=>

string(1) "C"

[3]=>

string(1) "D"

}

["two"]=>

array(4) {

[0]=>

string(2) "BB"

[1]=>

string(2) "AA"

[2]=>

string(2) "CC"

[3]=>

string(2) "DD"

}

}

#4

0

The best practice, imho, is as follows:

imho的最佳實踐如下:

$tmp = array_combine($array['one'],$array['two']);

shuffle($tmp);

$array['one'] = array_keys($tmp);

$array['two'] = array_values($tmp);

This is clear code and should be fast. No need to reinvent the wheel like in some other answers.

這是明確的代碼,應該很快。不需要像其他答案一樣重新發明輪子。

#5

0

This isn't an example with multidimensional arrays but it works great for sorting multiple normal arrays the same way and with shuffle. Maybe it could be adapted for multidimensional arrays too. It does assume all arrays are the same length.

這不是多維數組的示例,但它適用於以相同方式和shuffle排序多個普通數組。也許它也適用於多維數組。它確實假設所有數組的長度相同。

$array_count = count($array_1);

for($i=0;$i<=($array_count-1);$i++){

$temp_array[$i] = $i;

}

shuffle($temp_array);

for($i=0;$i<=($array_count-1);$i++){

$o = $temp_array[$i];

$array_1_sorted[$i]=$array_1[$o];

$array_2_sorted[$i]=$array_2[$o];

$array_3_sorted[$i]=$array_3[$o];

}

This will give you new arrays which are all sorted the same random way. You could then set the old arrays equal to the new ones or keep them in-tact.

這將為您提供新的數組,這些數組都以相同的隨機方式排序。然后,您可以將舊數組設置為等於新數組,或者保持它們不受影響。

#6

-1

Not sure exactly what you are trying to do, but your best bet is probably to put array 1 and array 2 into a multi-dimensional array and then random the primary array. That will give you a random arrangement of values, but within each key will be the values of each array. We could give you more specific answers if you can provide a bit more detail of exactly what you are doing.

不確定你想要做什么,但你最好的選擇可能是將數組1和數組2放入一個多維數組,然后將主數組隨機。這將為您提供隨機排列的值,但每個鍵中將是每個數組的值。如果您能提供更准確的信息,我們可以為您提供更具體的答案。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值