"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
WBOY원래의
2016-06-13 11:57:56946검색

问个二位数组操作问题
$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 />

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.