欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2023-12-26 12:53:39
...
问题有点绕,头晕者勿进^_^
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);
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
)

)

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

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频


上一篇:

下一篇: