Rumah >pembangunan bahagian belakang >tutorial php >两个数组合并有关问题
两个数组合并问题
<br />$arr1=Array<br /> (<br /> [0] => Array<br /> (<br /> [time] => 8:00<br /> [detail] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 31<br /> [casename] => 张三<br /> <br /> )<br /> [1] => Array<br /> (<br /> [caseid] => 32<br /> [casename] =>李四<br /> <br /> )<br /> [2] => Array<br /> (<br /> [caseid] => 33<br /> [casename] => 王五<br /> <br /> )<br /> <br /> )<br /> <br /> )<br /> <br /> [1] => Array<br /> (<br /> [time] => 12:00<br /> [detail] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 32<br /> [casename] => 李四 <br /> )<br /> [1] => Array<br /> (<br /> [caseid] => 33<br /> [casename] => 王五 <br /> )<br /> <br /> )<br /> <br /> )<br /> <br /> [2] => Array<br /> (<br /> [time] => 18:00<br /> [detail] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 31<br /> [casename] => 张三<br /> <br /> )<br /> [1] => Array<br /> (<br /> [caseid] => 32<br /> [casename] => 李四<br /> <br /> )<br /> [2] => Array<br /> (<br /> [caseid] => 33<br /> [casename] => 王五<br /> <br /> )<br /> <br /> )<br /> <br /> )<br /> <br /> )<br /><br />
<br />$arr2=Array<br /> (<br /> [0] => Array<br /> (<br /> [time] => 8:00<br /> [detail2] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 41<br /> [casename] => 刘备<br /> <br /> )<br /> <br /> )<br /> <br /> )<br /> <br /> [1] => Array<br /> (<br /> [time] => 16:00<br /> [detail2] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 42<br /> [casename] => 张飞 <br /> )<br /> [1] => Array<br /> (<br /> [caseid] => 43<br /> [casename] => 赵云 <br /> )<br /> <br /> )<br /> <br /> )<br /> <br /> [2] => Array<br /> (<br /> [time] => 18:00<br /> [detail2] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 43<br /> [casename] => 赵云<br /> <br /> )<br /> <br /> )<br /> <br /> )<br /> [2] => Array<br /> (<br /> [time] => 22:00<br /> [detail2] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 45<br /> [casename] => 貂蝉<br /> <br /> )<br /> <br /> )<br /> <br /> )<br /> <br /> )<br /><br />合并后,最终数组应该是这样显示:<br />[code=php]<br />$result=Array<br /> (<br /> [0] => Array<br /> (<br /> [time] => 8:00<br /> [detail] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 31<br /> [casename] => 张三<br /> <br /> )<br /> [1] => Array<br /> (<br /> [caseid] => 32<br /> [casename] =>李四<br /> <br /> )<br /> [2] => Array<br /> (<br /> [caseid] => 33<br /> [casename] => 王五<br /> <br /> )<br /> <br /> )<br /> [detail2] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 41<br /> [casename] => 刘备<br /> <br /> )<br /> <br /> )<br /> <br /> )<br /> <br /> [1] => Array<br /> (<br /> [time] => 12:00<br /> [detail] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 32<br /> [casename] => 李四 <br /> )<br /> [1] => Array<br /> (<br /> [caseid] => 33<br /> [casename] => 王五 <br /> )<br /> <br /> )<br /> [detail2] => Array<br /> (<br /><br /> )<br /> <br /> )<br /> [2] => Array<br /> (<br /> [time] => 16:00<br /> [detail] => Array<br /> (<br /><br /> )<br /> [detail2] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 42<br /> [casename] => 张飞 <br /> )<br /> [1] => Array<br /> (<br /> [caseid] => 43<br /> [casename] => 赵云 <br /> )<br /> <br /> )<br /> <br /> )<br /><br /> <br /> [3] => Array<br /> (<br /> [time] => 18:00<br /> [detail] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 31<br /> [casename] => 张三<br /> <br /> )<br /> [1] => Array<br /> (<br /> [caseid] => 32<br /> [casename] => 李四<br /> <br /> )<br /> [2] => Array<br /> (<br /> [caseid] => 33<br /> [casename] => 王五<br /> <br /> )<br /> <br /> )<br /> [detail2] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 43<br /> [casename] => 赵云<br /> <br /> )<br /> <br /> )<br /> <br /> )<br /> [4] => Array<br /> (<br /> [time] => 22:00<br /> [detail] => Array<br /> (<br /><br /> )<br /> [detail2] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 45<br /> [casename] => 貂蝉<br /> <br /> )<br /> <br /> )<br /> <br /> )<br /> <br /> )<br />
$arr1 = array (<br /> 0 => <br /> array (<br /> 'time' => '8:00',<br /> 'detail' => <br /> array (<br /> 0 => <br /> array (<br /> 'caseid' => '31',<br /> 'casename' => '张三',<br /> ),<br /> 1 => <br /> array (<br /> 'caseid' => '32',<br /> 'casename' => '李四',<br /> ),<br /> 2 => <br /> array (<br /> 'caseid' => '33',<br /> 'casename' => '王五',<br /> ),<br /> ),<br /> ),<br /> 1 => <br /> array (<br /> 'time' => '12:00',<br /> 'detail' => <br /> array (<br /> 0 => <br /> array (<br /> 'caseid' => '32',<br /> 'casename' => '李四',<br /> ),<br /> 1 => <br /> array (<br /> 'caseid' => '33',<br /> 'casename' => '王五',<br /> ),<br /> ),<br /> ),<br /> 2 => <br /> array (<br /> 'time' => '18:00',<br /> 'detail' => <br /> array (<br /> 0 => <br /> array (<br /> 'caseid' => '31',<br /> 'casename' => '张三',<br /> ),<br /> 1 => <br /> array (<br /> 'caseid' => '32',<br /> 'casename' => '李四',<br /> ),<br /> 2 => <br /> array (<br /> 'caseid' => '33',<br /> 'casename' => '王五',<br /> ),<br /> ),<br /> ),<br />);<br />$arr2 = array (<br /> 0 => <br /> array (<br /> 'time' => '8:00',<br /> 'detail2' => <br /> array (<br /> 0 => <br /> array (<br /> 'caseid' => '41',<br /> 'casename' => '刘备',<br /> ),<br /> ),<br /> ),<br /> 1 => <br /> array (<br /> 'time' => '16:00',<br /> 'detail2' => <br /> array (<br /> 0 => <br /> array (<br /> 'caseid' => '42',<br /> 'casename' => '张飞',<br /> ),<br /> 1 => <br /> array (<br /> 'caseid' => '43',<br /> 'casename' => '赵云',<br /> ),<br /> ),<br /> ),<br /> 2 => <br /> array (<br /> 'time' => '22:00',<br /> 'detail2' => <br /> array (<br /> 0 => <br /> array (<br /> 'caseid' => '45',<br /> 'casename' => '貂蝉',<br /> ),<br /> ),<br /> ),<br />);<br />foreach($arr1 as $item) $a[$item['time']] = $item;<br />foreach($arr2 as $item) $b[$item['time']] = $item;<br />print_r(array_values(array_merge_recursive($a, $b)));<br />
Array<br />(<br /> [0] => Array<br /> (<br /> [time] => Array<br /> (<br /> [0] => 8:00<br /> [1] => 8:00<br /> )<br /><br /> [detail] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 31<br /> [casename] => 张三<br /> )<br /><br /> [1] => Array<br /> (<br /> [caseid] => 32<br /> [casename] => 李四<br /> )<br /><br /> [2] => Array<br /> (<br /> [caseid] => 33<br /> [casename] => 王五<br /> )<br /><br /> )<br /><br /> [detail2] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 41<br /> [casename] => 刘备<br /> )<br /><br /> )<br /><br /> )<br /><br /> [1] => Array<br /> (<br /> [time] => 12:00<br /> [detail] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 32<br /> [casename] => 李四<br /> )<br /><br /> [1] => Array<br /> (<br /> [caseid] => 33<br /> [casename] => 王五<br /> )<br /><br /> )<br /><br /> )<br /><br /> [2] => Array<br /> (<br /> [time] => 18:00<br /> [detail] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 31<br /> [casename] => 张三<br /> )<br /><br /> [1] => Array<br /> (<br /> [caseid] => 32<br /> [casename] => 李四<br /> )<br /><br /> [2] => Array<br /> (<br /> [caseid] => 33<br /> [casename] => 王五<br /> )<br /><br /> )<br /><br /> )<br /><br /> [3] => Array<br /> (<br /> [time] => 16:00<br /> [detail2] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 42<br /> [casename] => 张飞<br /> )<br /><br /> [1] => Array<br /> (<br /> [caseid] => 43<br /> [casename] => 赵云<br /> )<br /><br /> )<br /><br /> )<br /><br /> [4] => Array<br /> (<br /> [time] => 22:00<br /> [detail2] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 45<br /> [casename] => 貂蝉<br /> )<br /><br /> )<br /><br /> )<br /><br />)<br /><br />
<br /><?php<br />echo '<meta http-equiv="content-type" content="text/html;charset=utf-8">';<br />$arr1 = array (<br /> 0 => <br /> array (<br /> 'time' => '8:00',<br /> 'detail' => <br /> array (<br /> 0 => <br /> array (<br /> 'caseid' => '31',<br /> 'casename' => '张三',<br /> ),<br /> 1 => <br /> array (<br /> 'caseid' => '32',<br /> 'casename' => '李四',<br /> ),<br /> 2 => <br /> array (<br /> 'caseid' => '33',<br /> 'casename' => '王五',<br /> ),<br /> ),<br /> ),<br /> 1 => <br /> array (<br /> 'time' => '12:00',<br /> 'detail' => <br /> array (<br /> 0 => <br /> array (<br /> 'caseid' => '32',<br /> 'casename' => '李四',<br /> ),<br /> 1 => <br /> array (<br /> 'caseid' => '33',<br /> 'casename' => '王五',<br /> ),<br /> ),<br /> ),<br /> 2 => <br /> array (<br /> 'time' => '18:00',<br /> 'detail' => <br /> array (<br /> 0 => <br /> array (<br /> 'caseid' => '31',<br /> 'casename' => '张三',<br /> ),<br /> 1 => <br /> array (<br /> 'caseid' => '32',<br /> 'casename' => '李四',<br /> ),<br /> 2 => <br /> array (<br /> 'caseid' => '33',<br /> 'casename' => '王五',<br /> ),<br /> ),<br /> ),<br />);<br />$arr2 = array (<br /> 0 => <br /> array (<br /> 'time' => '8:00',<br /> 'detail2' => <br /> array (<br /> 0 => <br /> array (<br /> 'caseid' => '41',<br /> 'casename' => '刘备',<br /> ),<br /> ),<br /> ),<br /> 1 => <br /> array (<br /> 'time' => '16:00',<br /> 'detail2' => <br /> array (<br /> 0 => <br /> array (<br /> 'caseid' => '42',<br /> 'casename' => '张飞',<br /> ),<br /> 1 => <br /> array (<br /> 'caseid' => '43',<br /> 'casename' => '赵云',<br /> ),<br /> ),<br /> ),<br /> 2 => <br /> array (<br /> 'time' => '18:00',<br /> 'detail2' => <br /> array (<br /> 0 => <br /> array (<br /> 'caseid' => '43',<br /> 'casename' => '赵云',<br /> ),<br /> ),<br /> ),<br /> 3 => <br /> array (<br /> 'time' => '22:00',<br /> 'detail2' => <br /> array (<br /> 0 => <br /> array (<br /> 'caseid' => '45',<br /> 'casename' => '貂蝉',<br /> ),<br /> ),<br /> ),<br />);<br /><br />$result = array();<br /><br />foreach($arr1 as $row){<br /> $key = $row['time'];<br /> $row['detail2'] = array();<br /> $result[$key] = $row;<br />}<br /><br />foreach($arr2 as $row){<br /> $key = $row['time'];<br /> if(!isset($result[$key])){<br /> $result[$key] = array(<br /> 'time' => $row['time'],<br /> 'detail' => array(),<br /> 'detail2' => $row['detail2']<br /> );<br /> }else{<br /> $result[$key]['detail2'] = $row['detail2'];<br /> }<br />}<br /><br />ksort($result, SORT_NUMERIC);<br /><br />$result = array_values($result);<br /><br />echo '<pre class="brush:php;toolbar:false">';<br />print_r($result);<br />echo '';
<br />Array<br />(<br /> [0] => Array<br /> (<br /> [time] => 8:00<br /> [detail] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 31<br /> [casename] => 张三<br /> )<br /><br /> [1] => Array<br /> (<br /> [caseid] => 32<br /> [casename] => 李四<br /> )<br /><br /> [2] => Array<br /> (<br /> [caseid] => 33<br /> [casename] => 王五<br /> )<br /><br /> )<br /><br /> [detail2] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 41<br /> [casename] => 刘备<br /> )<br /><br /> )<br /><br /> )<br /><br /> [1] => Array<br /> (<br /> [time] => 12:00<br /> [detail] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 32<br /> [casename] => 李四<br /> )<br /><br /> [1] => Array<br /> (<br /> [caseid] => 33<br /> [casename] => 王五<br /> )<br /><br /> )<br /><br /> [detail2] => Array<br /> (<br /> )<br /><br /> )<br /><br /> [2] => Array<br /> (<br /> [time] => 16:00<br /> [detail] => Array<br /> (<br /> )<br /><br /> [detail2] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 42<br /> [casename] => 张飞<br /> )<br /><br /> [1] => Array<br /> (<br /> [caseid] => 43<br /> [casename] => 赵云<br /> )<br /><br /> )<br /><br /> )<br /><br /> [3] => Array<br /> (<br /> [time] => 18:00<br /> [detail] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 31<br /> [casename] => 张三<br /> )<br /><br /> [1] => Array<br /> (<br /> [caseid] => 32<br /> [casename] => 李四<br /> )<br /><br /> [2] => Array<br /> (<br /> [caseid] => 33<br /> [casename] => 王五<br /> )<br /><br /> )<br /><br /> [detail2] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 43<br /> [casename] => 赵云<br /> )<br /><br /> )<br /><br /> )<br /><br /> [4] => Array<br /> (<br /> [time] => 22:00<br /> [detail] => Array<br /> (<br /> )<br /><br /> [detail2] => Array<br /> (<br /> [0] => Array<br /> (<br /> [caseid] => 45<br /> [casename] => 貂蝉<br /> )<br /><br /> )<br /><br /> )<br /><br />)<br />