阵列PHP的置换 [英] Permutation of Array PHP
本文介绍了阵列PHP的置换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下问题:
$ multidmimensional =阵列( [0] =>阵列(
[0] => 1,
[1] => 2,
[2] => 3
);
[1] =>阵列(
[0] => 5,
[1] => 6,
[2] => 7
); ... [2] =>阵列(
[0] =&GT,4
[1] => 5,
););
我可以有一个或多个(嵌套的)阵列,并允许以一个例子的前两个以上数组:
我应该排列替换它们以下列方式:
15
16
1725
26
2736
37
38
如果我有,例如这三个阵列,我应该得到这样的结果:
154
164
174
155
165
175254
264
274
255
265
275364
374
384
365
375
385
我有一些问题,使一个算法,将解决这个问题。谁能帮我?
先谢谢了。
解决方案
$数据=阵列
(
'1'=>阵列(5,6,7),
'2'= GT;阵列(9,25,14));为($ I = 0; $ I< =计数(array_keys($数据)); $ I ++){
为($ J = 1; $ J< = 2; $ J ++){
$值[$ i] [] = $数据[$ J] [$ i];
}
}为($ I = 0; $ I<计数($值); $ I ++){
洗牌($值[$ i]);
}$ newData =阵列();
为($ I = 0; $ I 3;; $ I ++){
为($ J = 1; $ J< = 2; $ J ++){
$ newData [$ J] [] = array_pop($值[$ i]);
}
}
的print_r($ newData);
I have the following problem:
$multidmimensional = array(
[0] => array(
[0] => 1,
[1] => 2,
[2] => 3
);
[1] => array(
[0] => 5,
[1] => 6,
[2] => 7
);
...
[2] => array(
[0] =>,4
[1] => 5,
);
);
I can have one or more (nested) arrays, and lets take as an example the first two of the above arrays:
I should permutate them in the following way:
15
16
17
25
26
27
36
37
38
If I had for example those three arrays, I should get a result like this:
154
164
174
155
165
175
254
264
274
255
265
275
364
374
384
365
375
385
I am having some problems to make an algorithm that would fix this problem. Can anyone help me? Thanks in advance.
解决方案
$data = array
(
'1' => array(5, 6, 7),
'2' => array(9, 25, 14)
);
for($i=0; $i<=count(array_keys($data)); $i++) {
for($j=1; $j<=2; $j++) {
$values[$i][] = $data[$j][$i];
}
}
for($i=0; $i<count($values); $i++) {
shuffle($values[$i]);
}
$newData = array();
for($i=0; $i<3; $i++) {
for($j=1; $j<=2; $j++) {
$newData[$j][] = array_pop($values[$i]);
}
}
print_r($newData);
这篇关于阵列PHP的置换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文