首页 >后端开发 >php教程 >PHP数组操作问题

PHP数组操作问题

WBOY
WBOY原创
2016-06-23 13:57:33764浏览

比方说有2个数组$a和$b

$a=array("1"=>"123","2"=>"1234","3"=>"1334");


$b=array("1"=>"123,"2"=>"1230");

怎么得到$c=array("2"=>"12234","3"=>“delete");







因为1对应的123 都是一样 去掉了   2对应的 因为$b比$a小 所以要保留$a的   3因为不存在 所以 后面变成"delete“


回复讨论(解决方案)

$a = array("1"=>"123","2"=>"1234","3"=>"1334");$b = array("1"=>"123","2"=>"1230");$c = array();foreach($a as $k=>$v) {  if(isset($b[$k])) {    if($b[$k] == $v) continue; //相同的不保存    $c[$k] = max($b[$k], $v); //不相同保留大的  }else $c[$k] = 'delete';}print_r($c);
Array
(
    [2] => 1234
    [3] => delete
)

$a = array("1"=>"123","2"=>"1234","3"=>"1334");$b = array("1"=>"123","2"=>"1230");$c = array();foreach($a as $key=>$val){if(isset($b[$key])){if($val!=$b[$key]){$c[$key] = $val>$b[$key]? $val : $b[$key];}}else{$c[$key] = 'delete';}}print_r($c);

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