php 数组处理
程序员文章站
2022-05-31 12:14:01
...
$a:
array (size=16)
0 => string '1' (length=1)
1 => string '3' (length=1)
2 => string '5' (length=1)
3 => string '7' (length=1)
4 => string '10' (length=2)
5 => string '12' (length=2)
6 => string '14' (length=2)
7 => string '16' (length=2)
8 => string '17' (length=2)
9 => string '19' (length=2)
10 => string '21' (length=2)
11 => string '23' (length=2)
12 => string '26' (length=2)
13 => string '28' (length=2)
14 => string '30' (length=2)
15 => string '32' (length=2)
$b:
array (size=16)
0 => string '2' (length=1)
1 => string '4' (length=1)
2 => string '6' (length=1)
3 => string '8' (length=1)
4 => string '9' (length=1)
5 => string '11' (length=2)
6 => string '13' (length=2)
7 => string '15' (length=2)
8 => string '18' (length=2)
9 => string '20' (length=2)
10 => string '22' (length=2)
11 => string '24' (length=2)
12 => string '25' (length=2)
13 => string '27' (length=2)
14 => string '29' (length=2)
15 => string '31' (length=2)
如上数组。我要得到 [16, 1, 13, 4] [14, 3, 15, 2] [12, 5, 9, 8] [10,7, 9 , 8]
[32,17,29,20] [30,19,31,18] [28,21,25,24] [26,23,27,22]
array (size=16)
0 => string '1' (length=1)
1 => string '3' (length=1)
2 => string '5' (length=1)
3 => string '7' (length=1)
4 => string '10' (length=2)
5 => string '12' (length=2)
6 => string '14' (length=2)
7 => string '16' (length=2)
8 => string '17' (length=2)
9 => string '19' (length=2)
10 => string '21' (length=2)
11 => string '23' (length=2)
12 => string '26' (length=2)
13 => string '28' (length=2)
14 => string '30' (length=2)
15 => string '32' (length=2)
$b:
array (size=16)
0 => string '2' (length=1)
1 => string '4' (length=1)
2 => string '6' (length=1)
3 => string '8' (length=1)
4 => string '9' (length=1)
5 => string '11' (length=2)
6 => string '13' (length=2)
7 => string '15' (length=2)
8 => string '18' (length=2)
9 => string '20' (length=2)
10 => string '22' (length=2)
11 => string '24' (length=2)
12 => string '25' (length=2)
13 => string '27' (length=2)
14 => string '29' (length=2)
15 => string '31' (length=2)
如上数组。我要得到 [16, 1, 13, 4] [14, 3, 15, 2] [12, 5, 9, 8] [10,7, 9 , 8]
[32,17,29,20] [30,19,31,18] [28,21,25,24] [26,23,27,22]
回复讨论(解决方案)
看不出有什么组装规律
/*规律$a = array(1,3,5,7, 10,12,14,16, 17,19,21,23, 26,28,30,32); a1 a2 a3 a4$b = array(2,4,6,8, 9,11,13,15, 18,20,22,24, 25,27,29,31); b1 b2 b3 b4[16, 1,13, 4][14, 3,15, 2][12, 5, 9, 8][10, 7, 9 ,8]a2 + a1 = b2 + b1 = 17[32,17,29,20][30,19,31,18][28,21,25,24][26,23,27,22]a4 + a3 = b4 + b3 = 49*/t(17);t(49);function t($n){ $a = array(1,3,5,7, 10,12,14,16, 17,19,21,23, 26,28,30,32); $b = array(2,4,6,8, 9,11,13,15, 18,20,22,24, 25,27,29,31); $a = array_chunk($a,4); $b = array_chunk($b,4); //15+16 = 31 if($n $v){ echo join(' , ',array_merge($v,$data['b'][$k])).'
'; }}/*16 , 1 , 15 , 214 , 3 , 13 , 412 , 5 , 11 , 610 , 7 , 9 , 832 , 17 , 31 , 1830 , 19 , 29 , 2028 , 21 , 27 , 2226 , 23 , 25 , 24*/
是吗?
[ 12, 5, 9, 8 ]
[ 10, 7, 9, 8 ]
怎么解释?
[ 10, 7, 9, 8 ] 应该是[10,7, 11 , 6]
最后结果是两个数组的组合
a7 a0 b6 b1
a6 a1 b7 b0
a5 a2 b4 b3
a4 a3 b5 b2
a15 a8 b14 b9
a14 a9 b15 b8
a13 a10 b12 b11
a12 a11 b13 b10
[ 12, 5, 9, 8 ]
[ 10, 7, 9, 8 ]
感觉是他写错了,不然就没规律了
好吧,就算他写错了
那么
他要 [ 30, 19, 31, 18 ] 一组
你给 [ 30, 19, 29, 20 ] 一组
也是他写错了?
[ 12, 5, 9, 8 ]
[ 10, 7, 9, 8 ]
感觉是他写错了,不然就没规律了
嗯,感觉这样就不是规律了,有局限性
t(17);t(49);function t($n){ $a = array(1,3,5,7, 10,12,14,16, 17,19,21,23, 26,28,30,32); $b = array(2,4,6,8, 9,11,13,15, 18,20,22,24, 25,27,29,31); $a = array_chunk($a,4); $b = array_chunk($b,4); //15+16 = 31 if($n $v){ echo join(' , ',array_merge($v,$data['b'][$k])).'
'; }}/*16 , 1 , 13 , 414 , 3 , 15 , 212 , 5 , 9 , 810 , 7 , 11 , 632 , 17 , 29 , 2030 , 19 , 31 , 1828 , 21 , 25 , 2426 , 23 , 27 , 22*/
我想应该这样做
$a = array( 1, 3, 5, 7, 10, 12, 14, 16, 17, 19, 21, 23, 26, 28, 30, 32);$b = array( 2, 4, 6, 8, 9, 11, 13, 15, 18, 20, 22, 24, 25, 27, 29, 31);function foo($a) { $r = array(); foreach(array_chunk($a, 8) as $v) { $t = array_chunk($v, 4); $t = array_map(null, array_reverse($t[1]), $t[0]); $r = array_merge($r, $t); } return $r;}$r = foo($a);foreach(foo($b) as $k=>$v) { $k += $k % 2 ? -1 : 1; $r[$k] = array_merge($r[$k], $v);}print_r($r);
Array( [0] => Array ( [0] => 16 [1] => 1 [2] => 13 [3] => 4 ) [1] => Array ( [0] => 14 [1] => 3 [2] => 15 [3] => 2 ) [2] => Array ( [0] => 12 [1] => 5 [2] => 9 [3] => 8 ) [3] => Array ( [0] => 10 [1] => 7 [2] => 11 [3] => 6 ) [4] => Array ( [0] => 32 [1] => 17 [2] => 29 [3] => 20 ) [5] => Array ( [0] => 30 [1] => 19 [2] => 31 [3] => 18 ) [6] => Array ( [0] => 28 [1] => 21 [2] => 25 [3] => 24 ) [7] => Array ( [0] => 26 [1] => 23 [2] => 27 [3] => 22 ))只是调动位置
确实是调换位置,这个有规律些
[12, 5, 9, 8] [10,7, 9 , 8] 是我写错,不好意思。宗各位所述已经解决谢谢你们。
上一篇: 鼻塞怎么办 6款茶品巧治鼻塞
下一篇: 宝宝拉肚子的症状 宝宝拉肚子怎么办
推荐阅读
-
批量处理JDBC语句提高处理速度
-
华硕Win10变形本Transformer3发布 搭载七代Intel酷睿处理器
-
小米笔记本将于7月27日发布 Atom处理器+win10系统
-
小米笔记本大曝光:低压低功耗的Intel Skylake-U系列处理器
-
海尔T6 3笔记本怎么拆机更换cpu处理器?
-
edius处理视频很卡该怎么办? edius很卡的原因分析
-
php创建带有效期的linux账户
-
Microsoft .Net Remoting系列教程之三:Remoting事件处理全接触
-
使用sp_xml_preparedocument处理XML文档的方法
-
自动备份mssql server数据库并压缩的批处理脚本