Rumah > pembangunan bahagian belakang > tutorial php > 问题有点绕,头晕者勿进^_^

问题有点绕,头晕者勿进^_^

WBOY
Lepaskan: 2016-06-23 13:50:50
asal
951 orang telah melayarinya

array ( 0 => array ( 'id' => '10', 'title' => 'a','type' => '1', 'bian'=>'1'), 
 1 => array ( 'id' => '10', 'title' => 'a','type' => '1', 'bian'=>2), 
 2 => array ( 'id' => '11', 'title' => 'b','type' => '2', 'bian'=>'1'),
 3 => array ( 'id' => '11', 'title' => 'c','type' => '2', 'bian'=>'2'),  
 )
 当type=1去除相同id重复并且key变成ID,当type=2时KEY变成id_bian的形式,最终转成以下数组
 array ( 0 => array ( '10' => '', '11_1' => '','11_2' => ''),)
 然后在与下面数组合并
 array ( 0 => array ( '10' => '1','11_2' => '1'), 
 1 => array ( '10' => '1','11_2' => '1'),
 2 => array ( '11_1' => '1','11_2' => '1'),
 3 => array ( '10' => '2','11_2' => '1'),  
 )
 最终得到
  array ( 0 => array ( '10' => '1','11_1'=>'','11_2' => '1'), 
 1 => array ( '10' => '1', '11_1 => '', 11_2' => '1'),
 2 => array ( '10' => '', '11_1' => '1', '11_2' => '1'),
 3 => array ( '10' => '2', '11_1' => '', '11_2' => '1'),  
 )


回复讨论(解决方案)

$a = array ( 0 => array ( 'id' => '10', 'title' => 'a','type' => '1', 'bian'=>'1'),  1 => array ( 'id' => '10', 'title' => 'a','type' => '1', 'bian'=>'2'),  2 => array ( 'id' => '11', 'title' => 'b','type' => '2', 'bian'=>'1'), 3 => array ( 'id' => '11', 'title' => 'c','type' => '2', 'bian'=>'2'),  );$b = array ( 0 => array ( '10' => '1','11_2' => '1'),  1 => array ( '10' => '1','11_2' => '1'), 2 => array ( '11_1' => '1','11_2' => '1'), 3 => array ( '10' => '2','11_2' => '1'),  );$t = array();foreach($a as $item) {  if($item['type'] == '1') {    $t[$item['id']] = '';  }else $t[$item['id'].'_'.$item['bian']] = '';}$c = array();foreach($b as $v) {  $r = array();  foreach($t as $k=>$n) $r[$k] = @$v[$k];  $c[] = $r;}print_r($c);
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Array(    [0] => Array        (            [10] => 1            [11_1] =>             [11_2] => 1        )    [1] => Array        (            [10] => 1            [11_1] =>             [11_2] => 1        )    [2] => Array        (            [10] =>             [11_1] => 1            [11_2] => 1        )    [3] => Array        (            [10] => 2            [11_1] =>             [11_2] => 1        ))
Salin selepas log masuk
Salin selepas log masuk

$a = array ( 0 => array ( 'id' => '10', 'title' => 'a','type' => '1', 'bian'=>'1'),  1 => array ( 'id' => '10', 'title' => 'a','type' => '1', 'bian'=>'2'),  2 => array ( 'id' => '11', 'title' => 'b','type' => '2', 'bian'=>'1'), 3 => array ( 'id' => '11', 'title' => 'c','type' => '2', 'bian'=>'2'),  );$b = array ( 0 => array ( '10' => '1','11_2' => '1'),  1 => array ( '10' => '1','11_2' => '1'), 2 => array ( '11_1' => '1','11_2' => '1'), 3 => array ( '10' => '2','11_2' => '1'),  );$t = array();foreach($a as $item) {  if($item['type'] == '1') {    $t[$item['id']] = '';  }else $t[$item['id'].'_'.$item['bian']] = '';}$c = array();foreach($b as $v) {  $r = array();  foreach($t as $k=>$n) $r[$k] = @$v[$k];  $c[] = $r;}print_r($c);
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Array(    [0] => Array        (            [10] => 1            [11_1] =>             [11_2] => 1        )    [1] => Array        (            [10] => 1            [11_1] =>             [11_2] => 1        )    [2] => Array        (            [10] =>             [11_1] => 1            [11_2] => 1        )    [3] => Array        (            [10] => 2            [11_1] =>             [11_2] => 1        ))
Salin selepas log masuk
Salin selepas log masuk


如果在$C里追加$t做为元素是不是要重新遍历?
追加 array ( '10' => '', '11_1' => '','11_2' => '')做为$c的

不太明白你的意思

不太明白你的意思


$a = array ( 0 => array ( 'id' => '10', 'title' => 'a','type' => '1', 'bian'=>'1'),  1 => array ( 'id' => '10', 'title' => 'a','type' => '1', 'bian'=>'2'),  2 => array ( 'id' => '11', 'title' => 'b','type' => '2', 'bian'=>'1'), 3 => array ( 'id' => '11', 'title' => 'c','type' => '2', 'bian'=>'2'),  );$b = array ( 0 => array ( '10' => '1','11_2' => '1'),  1 => array ( '10' => '1','11_2' => '1'), 2 => array ( '11_1' => '1','11_2' => '1'), 3 => array ( '10' => '2','11_2' => '1'),  );$t = array();foreach($a as $item) {  if($item['type'] == '1') {    $t[$item['id']] = '';  }else $t[$item['id'].'_'.$item['bian']] = '';}$c = array();foreach($b as $v) {  $r = array();  foreach($t as $k=>$n) $r[$k] = @$v[$k];  $c[] = $r;}print_r($c);
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

就是数组a整里的结果插到$c中,是不是一定要把$c在遍历一次?

并没有遍历 $c 啊

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