Home > Backend Development > PHP Tutorial > 有关问题有点绕,头晕者勿进^

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

WBOY
Release: 2016-06-13 12:05:40
Original
952 people have browsed it

问题有点绕,头晕者勿进^_^
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 (<br /> 0 => array ( 'id' => '10', 'title' => 'a','type' => '1', 'bian'=>'1'), <br /> 1 => array ( 'id' => '10', 'title' => 'a','type' => '1', 'bian'=>'2'), <br /> 2 => array ( 'id' => '11', 'title' => 'b','type' => '2', 'bian'=>'1'),<br /> 3 => array ( 'id' => '11', 'title' => 'c','type' => '2', 'bian'=>'2'),  <br />);<br />$b = array (<br /> 0 => array ( '10' => '1','11_2' => '1'), <br /> 1 => array ( '10' => '1','11_2' => '1'),<br /> 2 => array ( '11_1' => '1','11_2' => '1'),<br /> 3 => array ( '10' => '2','11_2' => '1'),  <br />);<br />$t = array();<br />foreach($a as $item) {<br />  if($item['type'] == '1') {<br />    $t[$item['id']] = '';<br />  }else $t[$item['id'].'_'.$item['bian']] = '';<br />}<br />$c = array();<br />foreach($b as $v) {<br />  $r = array();<br />  foreach($t as $k=>$n) $r[$k] = @$v[$k];<br />  $c[] = $r;<br />}<br />print_r($c);
Copy after login
Array<br />(<br />    [0] => Array<br />        (<br />            [10] => 1<br />            [11_1] => <br />            [11_2] => 1<br />        )<br /><br />    [1] => Array<br />        (<br />            [10] => 1<br />            [11_1] => <br />            [11_2] => 1<br />        )<br /><br />    [2] => Array<br />        (<br />            [10] => <br />            [11_1] => 1<br />            [11_2] => 1<br />        )<br /><br />    [3] => Array<br />        (<br />            [10] => 2<br />            [11_1] => <br />            [11_2] => 1<br />        )<br /><br />)<br /><br />
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template