The problem is a bit difficult. do not go to the php Tutorial if you are dizzy.

Source: Internet
Author: User
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.

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.