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

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

程序员文章站 2024-02-02 22:31:40
...
问个二位数组操作问题
$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(
array("id"=>"1","time"=>"5","neirong"=>"www"),
array("id"=>"2","time"=>"55","neirong"=>"www"),
array("id"=>"4","time"=>"555","neirong"=>"www"),
array("id"=>"5","time"=>"555","neirong"=>"www"),
);

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

foreach($aa as $v) $r[$v['id']] = $v;
foreach($bb as $v) {
$k = $v['id'];
if(! isset($r[$k])) $r[$k] = $v;
elseif($r[$k]['time'] elseif($r[$k]['time'] == $v['time']) unset($r[$k]);
}
ksort($r);
print_r($r);
Array
(
[2] => Array
(
[id] => 2
[time] => 55
[neirong] => www
)

[3] => Array
(
[id] => 3
[time] => 555
)

[4] => Array
(
[id] => 4
[time] => 555
[neirong] => www
)

[5] => Array
(
[id] => 5
[time] => 555
[neirong] => www
)

)

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

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

相关文章

相关视频