php 数组处理

WBOY
Lepaskan: 2016-06-23 13:06:04
asal
857 orang telah melayarinya

$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*/
Salin selepas log masuk

是吗?
[ 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*/
Salin selepas log masuk

我想应该这样做

$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);
Salin selepas log masuk
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        ))
Salin selepas log masuk
只是调动位置

确实是调换位置,这个有规律些

[12,  5,  9,  8]   [10,7, 9 ,     8] 是我写错,不好意思。宗各位所述已经解决谢谢你们。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan