Home > Backend Development > PHP Tutorial > 数组操作问题

数组操作问题

WBOY
Release: 2016-06-23 13:56:52
Original
752 people have browsed it

$bb=array("id"=>"1","wo2"=>"8")
$aa=array("id"=>"1","wo2"=>"5","wo3"=>"3")



2个数组如何操作  变成$cc=array("id"=>"1","wo2"=>"8","wo3"=>"delete")
      因为 wo2  aa比bb小 所以 是8  如何相反就是5   wo3  因为bb不存在所有delete了


回复讨论(解决方案)


$a=count($aa);
$b=count($bb);
$ar=$a>b?$aa:$bb;
$ar2=$a foreach($ar as $k=>$v){
 $cc[$k]=isset($ar2[$k])&&$ar2[$k]>$v?$ar2[$k]:$v;

}

$cc[$k]=isset($ar2[$k])&&$ar2[$k]>$v?$ar2[$k]:'delete';

<?php$bb=array("id"=>"1","wo2"=>"8");$aa=array("id"=>"1","wo2"=>"5","wo3"=>"3");//简单的处理foreach ($aa as $key => &$value) {  if(isset($bb[$key])){    $aa[$key] = $aa[$key]>$bb[$key]?$aa[$key]:$bb[$key];  }else{    $aa[$key] = "delete";  }}var_dump($aa);$aa=array("id"=>"1","wo2"=>"5","wo3"=>"3");//第二种用回调函数处理$bbkeys = array_keys($bb);array_walk($aa, function(&$item,$key) use($bb,$bbkeys){  if(in_array($key, $bbkeys)){    $item = $item>$bb[$key]?$item:$bb[$key];  }else{    $item="delete";  }});var_dump($aa);?>
Copy after login

$bb=array("id"=>"1","wo2"=>"8");
$aa=array("id"=>"1","wo2"=>"5","wo3"=>"3");

$out = array();
$cc = array_merge_recursive($bb,$aa);//两数组合并
foreach ($cc as $key => $value) {
if(!is_array($value)){
$out[$key] = 'delete';
}else{
$out[$key] = $value[0] >= $value[1] ? $value[0] : $value[1];
}
}
var_dump($out);

$bb=array("id"=>"1","wo2"=>"8");$aa=array("id"=>"1","wo2"=>"5","wo3"=>"3");$cc=array_merge($aa,$bb);$cc['wo3']='delete';print_r($cc);
Copy after login
Copy after login

Array(    [id] => 1    [wo2] => 8    [wo3] => delete)
Copy after login
Copy after login

$bb=array("id"=>"1","wo2"=>"8");$aa=array("id"=>"1","wo2"=>"5","wo3"=>"3");$cc=array_merge($aa,$bb);$cc['wo3']='delete';print_r($cc);
Copy after login
Copy after login

Array(    [id] => 1    [wo2] => 8    [wo3] => delete)
Copy after login
Copy after login

我其实挺喜欢你的代码。。但是你这个delete 这样哈。。这个都是动态的。。

$bb=array("id"=>"1","wo2"=>"8");
$aa=array("id"=>"1","wo2"=>"5","wo3"=>"3");

$out = array();
$cc = array_merge_recursive($bb,$aa);//两数组合并
foreach ($cc as $key => $value) {
if(!is_array($value)){
$out[$key] = 'delete';
}else{
$out[$key] = $value[0] >= $value[1] ? $value[0] : $value[1];
}
}
var_dump($out);

我喜欢你得算法 逻辑简单容易维护 哈哈 

$bb=array("id"=>"1","wo2"=>"8");
$aa=array("id"=>"1","wo2"=>"5","wo3"=>"3");

$out = array();
$cc = array_merge_recursive($bb,$aa);//两数组合并
foreach ($cc as $key => $value) {
if(!is_array($value)){
$out[$key] = 'delete';
}else{
$out[$key] = $value[0] >= $value[1] ? $value[0] : $value[1];
}
}
var_dump($out);



我在写缓存把每条记录都有个time字段 里面存得都是更新时间

我每次传服务器 需要把客户端的time字段内容和对应id发给服务器 跟服务器的time对比
如果客户端time小于服务器time 那服务器需要把这条内容下发给客户端。。。 下发都是采用replace into的方法操作数据库
如果大于或者等于 那就不用下发给客户端

如果客户端的id发给服务器端 服务器的id找不到这个id 那就服务器会下发个 客户端id 字段内容就是delete  来通知客户端已经删除 


比方说客户端是
$bb=array("id"=>"1","time"=>"8","neirong"=>"www");
比方说服务端是
$cc=array("id"=>"1","time"=>"8","neirong"=>"www");
$dd是个二维数组是需要下发给客户端的
因为 time一样所以不用下发到客户端 所以为空 

如果
比方说客户端是
$bb=array("id"=>"2","time"=>"5","neirong"=>"www");
比方说服务端是
$cc=array("id"=>"2","time"=>"8","neirong"=>"www");
那收到的是array("id"=>"1","time"=>"8","neirong"=>"www");  这个数组就添加到$dd 


如果
比方说客户端是
$bb=array("id"=>"3","time"=>"5","neirong"=>"www");
比方说服务端是
$cc=array();

那就下发
array("id"=>"1","neirong"=>"delete"); 也添加到$dd     每条数据对比完后  $dd就统一发给客户端
这个逻辑我应该怎么写 感觉好难

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template