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

问个二位数组操作问题

程序员文章站 2022-05-24 10:16:12
...
$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的数据。。所以





回复讨论(解决方案)

太基础了,回去重练...

太基础了,回去重练...

有本事你写个?

多维数组拼写起来确实费点时间,试着写了一个,不知道是不是楼主所要的结果
array (size=4)
0 =>
array (size=3)
'id' => string '3' (length=1)
'time' => string '555' (length=3)
'neirong' => string 'delete' (length=6)
1 =>
array (size=3)
'id' => string '2' (length=1)
'time' => string '55' (length=2)
'neirong' => string 'www' (length=3)
2 =>
array (size=3)
'id' => string '4' (length=1)
'time' => string '555' (length=3)
'neirong' => string 'www' (length=3)
3 =>
array (size=3)
'id' => string '5' (length=1)
'time' => string '555' (length=3)
'neirong' => string 'www' (length=3)

。。。。。。。。。。

$aa=array(0=>array("id"=>"1","time"=>"5","neirong"=>"www"),1=>array("id"=>"2","time"=>"55","neirong"=>"www"),2=>array("id"=>"4","time"=>"555","neirong"=>"www"),3=>array("id"=>"5","time"=>"555","neirong"=>"www"));$bb=array(0=>array("id"=>"1","time"=>"5"),1=>array("id"=>"2","time"=>"50"),2=>array("id"=>"3","time"=>"555"));$cc=array();for($i=0;$i$id,"neirong"=>"delete");}for($i=0;$i$bb[$j]['time']){                $cc[]=$aa[$i];                array_splice($aa,$i,1);            }        }    }}$cc=array_merge($cc,$aa);print_r($cc);


Array(    [0] => Array        (            [id] => 3            [neirong] => delete        )    [1] => Array        (            [id] => 2            [time] => 55            [neirong] => www        )    [2] => Array        (            [id] => 4            [time] => 555            [neirong] => www        )    [3] => Array        (            [id] => 5            [time] => 555            [neirong] => www        ))

$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']   
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        ))

$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']    
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        ))


[3] => Array
(
[id] => 3
[time] => 555
)
不对哈 没delete

[id] => 3
[neirong] => delete

你原始的数据只有 $bb=array("id"=>"3","time"=>"555");
我不能凭空臆造出 "neirong"=>"delete"
但你可以,因为规则是你制定的
你自己加上就是了

"1","time"=>"5","neirong"=>"www");$aa[1]=array("id"=>"2","time"=>"55","neirong"=>"www");$aa[2]=array("id"=>"4","time"=>"555","neirong"=>"www");$aa[3]=array("id"=>"5","time"=>"555","neirong"=>"www");$bb[0]=array("id"=>"1","time"=>"5");$bb[1]=array("id"=>"2","time"=>"50");$bb[2]=array("id"=>"3","time"=>"555");$c = array();$d = array();$e = array();$j = array();foreach($bb as $v){  $e[] = $v['id'];}foreach($aa as $v){  $d[] = $v['id'];}$s = array_diff_assoc($e, $d);$f = array_diff_assoc($d, $e);$g = array_merge($s,$f);foreach($aa as $k=>$v){  foreach($bb as $k1=>$v1){    if($v['id']==$v1['id'] && $v1['time']>$v['time']){      $c[] = array('id'=>$v1['id'],'time'=>$v1['time'],'neirong'=>'delete');    }elseif($v['id']==$v1['id'] && $v1['time']$v['id'],'time'=>$v['time'],'neirong'=>$v['neirong']);    }    foreach($g as $v2){      if($v['id'] == $v2 && !in_array($v['id'], $j)){        $c[] = array('id'=>$v['id'],'time'=>$v['time'],'neirong'=>$v['neirong']);        $j[] = $v['id'];      }      if($v1['id'] == $v2  && !in_array($v1['id'], $j)){        $c[] = array('id'=>$v1['id'],'time'=>$v1['time'],'neirong'=>'delete');        $j[] = $v1['id'];      }    }  }}var_dump($c);?>

我这个太麻烦了,自己可以改改,不太擅长简化代码

你原始的数据只有 $bb=array("id"=>"3","time"=>"555");
我不能凭空臆造出 "neirong"=>"delete"
但你可以,因为规则是你制定的
你自己加上就是了

哈哈我刚加上了

我这个太麻烦了,自己可以改改,不太擅长简化代码

刚看版主的代码是最好的 直接遍历$aa跟$bb对比修改

"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]))  {    $v['content']='delete';    $r[$k] = $v;  }   elseif($r[$k]['time'] s