The problem is a bit difficult. do not enter pai_^ 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 '),
)
When type = 1 removes duplicate IDs and the key is changed to id, when type = 2, the KEY is changed to id_bian format and is eventually converted to the following array
Array (0 => array ('10' => '', '11 _ 1' =>'', '11 _ 2' => ''),)
Then merge with the following array
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 '),
)
Finally
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 '),
)
Reply to discussion (solution)
$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);
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);
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 ))
If you append $ t to $ C as an element, do you want to traverse it again?
Append an array ('10' => '', '11 _ 1' =>'', '11 _ 2' => '') as the $ c
I don't quite understand what you mean
I don't quite understand what you mean
$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);
Is the result of array a inserted into $ c? is it necessary to traverse $ c once?
Not traversing $ c.