"1","time"=>"5","neirong"=>"www");$aa=array("id"=>"2","time"=>"55","neirong"=>"www");$aa=array("id"=>"4","time"=>"555","neirong""/> "1","time"=>"5","neirong"=>"www");$aa=array("id"=>"2","time"=>"55","neirong"=>"www");$aa=array("id"=>"4","time"=>"555","neirong"">
首页 > 后端开发 > php教程 > 问个二位数组操作有关问题

问个二位数组操作有关问题

WBOY
发布: 2016-06-13 11:57:56
原创
943 人浏览过

问个二位数组操作问题
$aa=array("id"=>"1","time"=>"5","neirong"=>"www");
$aa=array("id"=>"2","time"=>"55","neirong"=>"www");
$aa=array("id"=>"4","time"=>"555","neirong"=>"www");
$aa=array("id"=>"5","time"=>"555","neirong"=>"www");
$aaa=array("duibi"=>$aa);

$bb=array("id"=>"1","time"=>"5");
$bb=array("id"=>"2","time"=>"50");
$bb=array("id"=>"3","time"=>"555");
$bbb=array("duibi"=>$bb);

$aaa和$bbb对比操作 
变成
$cc=array("id"=>"2","time"=>"55","neirong"=>"www");
$cc=array("id"=>"3","neirong"=>"delete");
$cc=array("id"=>"4","time"=>"555","neirong"=>"www");
$cc=array("id"=>"5","time"=>"555","neirong"=>"www");

怎么写



先说明下

$aa=array("id"=>"1","time"=>"5","neirong"=>"www");这个数据之所已没有是因为 $aa和$bb时间是一样的 

$aa=array("id"=>"2","time"=>"55","neirong"=>"www");
$bb=array("id"=>"2","time"=>"50");
那是因为bbdetime比aa得time小所以。。

$cc=array("id"=>"3","neirong"=>"delete");对应的$aa不存在所以。。


$cc=array("id"=>"4","time"=>"555","neirong"=>"www");
$cc=array("id"=>"5","time"=>"555","neirong"=>"www");

因为$bb没有对应的id 4和5的数据。。所以





------解决方案--------------------

$aa = array(<br />  array("id"=>"1","time"=>"5","neirong"=>"www"),<br />  array("id"=>"2","time"=>"55","neirong"=>"www"),<br />  array("id"=>"4","time"=>"555","neirong"=>"www"),<br />  array("id"=>"5","time"=>"555","neirong"=>"www"),<br />);<br /><br />$bb = array(<br />  array("id"=>"1","time"=>"5"),<br />  array("id"=>"2","time"=>"50"),<br />  array("id"=>"3","time"=>"555"),<br />);<br /><br />foreach($aa as $v) $r[$v['id']] = $v;<br />foreach($bb as $v) {<br />  $k = $v['id'];<br />  if(! isset($r[$k])) $r[$k] = $v;<br />  elseif($r[$k]['time'] < $v['time']) $r[$k] = $v;<br />  elseif($r[$k]['time'] == $v['time']) unset($r[$k]);<br />}<br />ksort($r);<br />print_r($r);
登录后复制
Array<br />(<br />    [2] => Array<br />        (<br />            [id] => 2<br />            [time] => 55<br />            [neirong] => www<br />        )<br /><br />    [3] => Array<br />        (<br />            [id] => 3<br />            [time] => 555<br />        )<br /><br />    [4] => Array<br />        (<br />            [id] => 4<br />            [time] => 555<br />            [neirong] => www<br />        )<br /><br />    [5] => Array<br />        (<br />            [id] => 5<br />            [time] => 555<br />            [neirong] => www<br />        )<br /><br />)<br /><br />
登录后复制

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板