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

两个数组合并有关问题

程序员文章站 2022-05-24 18:14:33
...
两个数组合并问题
本帖最后由 bluezsh 于 2014-10-16 16:25:27 编辑 数组1:

$arr1=Array
(
[0] => Array
(
[time] => 8:00
[detail] => Array
(
[0] => Array
(
[caseid] => 31
[casename] => 张三

)
[1] => Array
(
[caseid] => 32
[casename] =>李四

)
[2] => Array
(
[caseid] => 33
[casename] => 王五

)

)

)

[1] => Array
(
[time] => 12:00
[detail] => Array
(
[0] => Array
(
[caseid] => 32
[casename] => 李四
)
[1] => Array
(
[caseid] => 33
[casename] => 王五
)

)

)

[2] => Array
(
[time] => 18:00
[detail] => Array
(
[0] => Array
(
[caseid] => 31
[casename] => 张三

)
[1] => Array
(
[caseid] => 32
[casename] => 李四

)
[2] => Array
(
[caseid] => 33
[casename] => 王五

)

)

)

)


数组2

$arr2=Array
(
[0] => Array
(
[time] => 8:00
[detail2] => Array
(
[0] => Array
(
[caseid] => 41
[casename] => 刘备

)

)

)

[1] => Array
(
[time] => 16:00
[detail2] => Array
(
[0] => Array
(
[caseid] => 42
[casename] => 张飞
)
[1] => Array
(
[caseid] => 43
[casename] => 赵云
)

)

)

[2] => Array
(
[time] => 18:00
[detail2] => Array
(
[0] => Array
(
[caseid] => 43
[casename] => 赵云

)

)

)
[2] => Array
(
[time] => 22:00
[detail2] => Array
(
[0] => Array
(
[caseid] => 45
[casename] => 貂蝉

)

)

)

)

合并后,最终数组应该是这样显示:
[code=php]
$result=Array
(
[0] => Array
(
[time] => 8:00
[detail] => Array
(
[0] => Array
(
[caseid] => 31
[casename] => 张三

)
[1] => Array
(
[caseid] => 32
[casename] =>李四

)
[2] => Array
(
[caseid] => 33
[casename] => 王五

)

)
[detail2] => Array
(
[0] => Array
(
[caseid] => 41
[casename] => 刘备

)

)

)

[1] => Array
(
[time] => 12:00
[detail] => Array
(
[0] => Array
(
[caseid] => 32
[casename] => 李四
)
[1] => Array
(
[caseid] => 33
[casename] => 王五
)

)
[detail2] => Array
(

)

)
[2] => Array
(
[time] => 16:00
[detail] => Array
(

)
[detail2] => Array
(
[0] => Array
(
[caseid] => 42
[casename] => 张飞
)
[1] => Array
(
[caseid] => 43
[casename] => 赵云
)

)

)


[3] => Array
(
[time] => 18:00
[detail] => Array
(
[0] => Array
(
[caseid] => 31
[casename] => 张三

)
[1] => Array
(
[caseid] => 32
[casename] => 李四

)
[2] => Array
(
[caseid] => 33
[casename] => 王五

)

)
[detail2] => Array
(
[0] => Array
(
[caseid] => 43
[casename] => 赵云

)

)

)
[4] => Array
(
[time] => 22:00
[detail] => Array
(

)
[detail2] => Array
(
[0] => Array
(
[caseid] => 45
[casename] => 貂蝉

)

)

)

)

[/code]
------解决思路----------------------
$arr1 = array (
0 =>
array (
'time' => '8:00',
'detail' =>
array (
0 =>
array (
'caseid' => '31',
'casename' => '张三',
),
1 =>
array (
'caseid' => '32',
'casename' => '李四',
),
2 =>
array (
'caseid' => '33',
'casename' => '王五',
),
),
),
1 =>
array (
'time' => '12:00',
'detail' =>
array (
0 =>
array (
'caseid' => '32',
'casename' => '李四',
),
1 =>
array (
'caseid' => '33',
'casename' => '王五',
),
),
),
2 =>
array (
'time' => '18:00',
'detail' =>
array (
0 =>
array (
'caseid' => '31',
'casename' => '张三',
),
1 =>
array (
'caseid' => '32',
'casename' => '李四',
),
2 =>
array (
'caseid' => '33',
'casename' => '王五',
),
),
),
);
$arr2 = array (
0 =>
array (
'time' => '8:00',
'detail2' =>
array (
0 =>
array (
'caseid' => '41',
'casename' => '刘备',
),
),
),
1 =>
array (
'time' => '16:00',
'detail2' =>
array (
0 =>
array (
'caseid' => '42',
'casename' => '张飞',
),
1 =>
array (
'caseid' => '43',
'casename' => '赵云',
),
),
),
2 =>
array (
'time' => '22:00',
'detail2' =>
array (
0 =>
array (
'caseid' => '45',
'casename' => '貂蝉',
),
),
),
);
foreach($arr1 as $item) $a[$item['time']] = $item;
foreach($arr2 as $item) $b[$item['time']] = $item;
print_r(array_values(array_merge_recursive($a, $b)));
Array
(
[0] => Array
(
[time] => Array
(
[0] => 8:00
[1] => 8:00
)

[detail] => Array
(
[0] => Array
(
[caseid] => 31
[casename] => 张三
)

[1] => Array
(
[caseid] => 32
[casename] => 李四
)

[2] => Array
(
[caseid] => 33
[casename] => 王五
)

)

[detail2] => Array
(
[0] => Array
(
[caseid] => 41
[casename] => 刘备
)

)

)

[1] => Array
(
[time] => 12:00
[detail] => Array
(
[0] => Array
(
[caseid] => 32
[casename] => 李四
)

[1] => Array
(
[caseid] => 33
[casename] => 王五
)

)

)

[2] => Array
(
[time] => 18:00
[detail] => Array
(
[0] => Array
(
[caseid] => 31
[casename] => 张三
)

[1] => Array
(
[caseid] => 32
[casename] => 李四
)

[2] => Array
(
[caseid] => 33
[casename] => 王五
)

)

)

[3] => Array
(
[time] => 16:00
[detail2] => Array
(
[0] => Array
(
[caseid] => 42
[casename] => 张飞
)

[1] => Array
(
[caseid] => 43
[casename] => 赵云
)

)

)

[4] => Array
(
[time] => 22:00
[detail2] => Array
(
[0] => Array
(
[caseid] => 45
[casename] => 貂蝉
)

)

)

)


php 提供了丰富的函数
你应该自己动动手
------解决思路----------------------
数组2有问题,18:00,与 22:00的下标都是2,把22:00改为3

echo '';
$arr1 = array (
0 =>
array (
'time' => '8:00',
'detail' =>
array (
0 =>
array (
'caseid' => '31',
'casename' => '张三',
),
1 =>
array (
'caseid' => '32',
'casename' => '李四',
),
2 =>
array (
'caseid' => '33',
'casename' => '王五',
),
),
),
1 =>
array (
'time' => '12:00',
'detail' =>
array (
0 =>
array (
'caseid' => '32',
'casename' => '李四',
),
1 =>
array (
'caseid' => '33',
'casename' => '王五',
),
),
),
2 =>
array (
'time' => '18:00',
'detail' =>
array (
0 =>
array (
'caseid' => '31',
'casename' => '张三',
),
1 =>
array (
'caseid' => '32',
'casename' => '李四',
),
2 =>
array (
'caseid' => '33',
'casename' => '王五',
),
),
),
);
$arr2 = array (
0 =>
array (
'time' => '8:00',
'detail2' =>
array (
0 =>
array (
'caseid' => '41',
'casename' => '刘备',
),
),
),
1 =>
array (
'time' => '16:00',
'detail2' =>
array (
0 =>
array (
'caseid' => '42',
'casename' => '张飞',
),
1 =>
array (
'caseid' => '43',
'casename' => '赵云',
),
),
),
2 =>
array (
'time' => '18:00',
'detail2' =>
array (
0 =>
array (
'caseid' => '43',
'casename' => '赵云',
),
),
),
3 =>
array (
'time' => '22:00',
'detail2' =>
array (
0 =>
array (
'caseid' => '45',
'casename' => '貂蝉',
),
),
),
);

$result = array();

foreach($arr1 as $row){
$key = $row['time'];
$row['detail2'] = array();
$result[$key] = $row;
}

foreach($arr2 as $row){
$key = $row['time'];
if(!isset($result[$key])){
$result[$key] = array(
'time' => $row['time'],
'detail' => array(),
'detail2' => $row['detail2']
);
}else{
$result[$key]['detail2'] = $row['detail2'];
}
}

ksort($result, SORT_NUMERIC);

$result = array_values($result);

echo '
';
print_r($result);
echo '
';

?>



Array
(
[0] => Array
(
[time] => 8:00
[detail] => Array
(
[0] => Array
(
[caseid] => 31
[casename] => 张三
)

[1] => Array
(
[caseid] => 32
[casename] => 李四
)

[2] => Array
(
[caseid] => 33
[casename] => 王五
)

)

[detail2] => Array
(
[0] => Array
(
[caseid] => 41
[casename] => 刘备
)

)

)

[1] => Array
(
[time] => 12:00
[detail] => Array
(
[0] => Array
(
[caseid] => 32
[casename] => 李四
)

[1] => Array
(
[caseid] => 33
[casename] => 王五
)

)

[detail2] => Array
(
)

)

[2] => Array
(
[time] => 16:00
[detail] => Array
(
)

[detail2] => Array
(
[0] => Array
(
[caseid] => 42
[casename] => 张飞
)

[1] => Array
(
[caseid] => 43
[casename] => 赵云
)

)

)

[3] => Array
(
[time] => 18:00
[detail] => Array
(
[0] => Array
(
[caseid] => 31
[casename] => 张三
)

[1] => Array
(
[caseid] => 32
[casename] => 李四
)

[2] => Array
(
[caseid] => 33
[casename] => 王五
)

)

[detail2] => Array
(
[0] => Array
(
[caseid] => 43
[casename] => 赵云
)

)

)

[4] => Array
(
[time] => 22:00
[detail] => Array
(
)

[detail2] => Array
(
[0] => Array
(
[caseid] => 45
[casename] => 貂蝉
)

)

)

)
两个数组合并有关问题

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

相关文章

相关视频


网友评论

文明上网理性发言,请遵守 新闻评论服务协议

我要评论
  • 两个数组合并有关问题
  • 专题推荐