php 数组处理
$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]
回复讨论(解决方案)
看不出有什么组装规律
/*规律$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 <= 31){ $useA = array_slice($a,0,2); $useB = array_slice($b,0,2); }else{ $useA = array_slice($a,2,4); $useB = array_slice($b,2,4); } $data = array(); arsort($useA[1]); foreach($useA[1] as $v1){ foreach($useA[0] as $v2){ if($n==$v1+$v2){ $tmp = array(); $tmp[] = $v1; $tmp[] = $v2; $data['a'][] = $tmp; } } } arsort($useB[1]); foreach($useB[1] as $v1){ foreach($useB[0] as $v2){ if($n==$v1+$v2){ $tmp = array(); $tmp[] = $v1; $tmp[] = $v2; $data['b'][] = $tmp; } } } foreach($data['a'] as $k=>$v){ echo join(' , ',array_merge($v,$data['b'][$k])).'<br>'; }}/*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 <= 31){ $useA = array_slice($a,0,2); $useB = array_slice($b,0,2); }else{ $useA = array_slice($a,2,4); $useB = array_slice($b,2,4); } $data = array(); arsort($useA[1]); foreach($useA[1] as $v1){ foreach($useA[0] as $v2){ if($n==$v1+$v2){ $tmp = array(); $tmp[] = $v1; $tmp[] = $v2; $data['a'][] = $tmp; } } } $tmp = array_slice($useB[1],0,2); $useB[1] = array_merge(array_slice($useB[1],2,4),$tmp); foreach($useB[1] as $v1){ foreach($useB[0] as $v2){ if($n==$v1+$v2){ $tmp = array(); $tmp[] = $v1; $tmp[] = $v2; $data['b'][] = $tmp; } } } foreach($data['a'] as $k=>$v){ echo join(' , ',array_merge($v,$data['b'][$k])).'<br>'; }}/*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] 是我写错,不好意思。宗各位所述已经解决谢谢你们。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.
