Php array processing

Source: Internet
Author: User
Php array processing $:
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)

The preceding array. I want to get [16, 1, 13, 4] [14, 3, 15, 2] [12, 5, 9, 8] [10, 7, 9, 8]
[,] [,] [,] [,] [,]


Reply to discussion (solution)

I don't see any assembly rules

/* Law $ a = array (,); a1 a2 a3 a4 $ B = array (, 11, ,); b1 b2 b3 b4 [16, 4] [14, 2] [12, 5, 9, 8] [10, 7, 9, 8] a2 + a1 = b2 + b1 = 17, 27,22] a4 + a3 = b4 + b3 = 49 */t (17); t (49); function t ($ n) {$ a = array (1, 3, 5, 7, ,); $ B = array ); $ a = array_chunk ($ a, 4); $ B = array_chunk ($ B, 4); // 15 + 16 = 31if ($ n <= 31) {$ useA = array_slice ($ a,); $ useB = array_slice ($ B,);} 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]).'
';}/* 16, 1, 15,214, 3, 13,412, 5, 11,610, 7, 9,832, 17, 31,183 0, 19, 29,202 8, 21, 27,222 6, 23, 25, 24 */

Yes?
[12, 5, 9, 8]
[10, 7, 9, 8]
How to explain it?

[10, 7, 9, 8] should be [10, 7, 11, 6]
The final result is a combination of two arrays.
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]
It seems that he has made a mistake. Otherwise, it will become irregular.

Okay, even if he says something wrong
So
He wants a group of [30, 19, 31, 18]
You give [30, 19, 29, 20] a group
He also wrote the error?

[12, 5, 9, 8]
[10, 7, 9, 8]
It seems that he has made a mistake. Otherwise, it will become irregular.

Well, it seems like this is not a regular rule, and it has limitations.

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 = 31if($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])).'
';}}/*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*/

I think we should do this.

$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        ))
Only transfer location

It is indeed a change of location, which is more regular

[12, 5, 9, 8] [10, 7, 9, 8] is an error. sorry. Thank you for your solution.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.