$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);?>
$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);
Array( [id] => 1 [wo2] => 8 [wo3] => delete)
$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);
Array( [id] => 1 [wo2] => 8 [wo3] => delete)我其实挺喜欢你的代码。。但是你这个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);