"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"">

Heim >Backend-Entwicklung >PHP-Tutorial >问个二位数组操作有关问题

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

WBOY
WBOYOriginal
2016-06-13 11:57:56946Durchsuche

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn