热搜:NVER 

二维数组判断值雷同

2024-04-29 10:54:01
 二维数组判断值雷同

二维数组判断值相同
$array = array(array('pid'=>1,'name'=>a),array('pid'=>2,'name'=>b),array('pid'=>3,'name'=>c),array('pid'=>1,'name'=>a),array('pid'=>4,'name'=>d));
类似这样的二维数组,怎样判断pid和name同时相等的值(如例子里的('pid'=>1,'name'=>a)值一样)
有的话就退出

------解决方案--------------------

PHP code

$array = array(
      array('pid'=>1,'name'=>'a'),
      array('pid'=>2,'name'=>'b'),
      array('pid'=>3,'name'=>'c'),
      array('pid'=>1,'name'=>'a')
);
$tmp=array();
foreach($array as $k=>$v){
       if(!$tem[$v[pid].'_'.$v[name]]){
              $tem[$v[pid].'_'.$v[name]]=$v;
              print_r($v);
        }else{
                exit;        
        }        
}

------解决方案--------------------
PHP code

$array = array(
    array('pid' => 1, 'name' => 'a'),
    array('pid' => 2, 'name' => 'b'),
    array('pid' => 3, 'name' => 'c'),
    array('pid' => 4, 'name' => 'd'),
    array('pid' => 5, 'name' => 'e'),
    array('pid' => 1, 'name' => 'a'),
    array('pid' => 4, 'name' => 'd')
);
$idList = array();
foreach ($array as $key => $value) {
    $idList[] = implode(',', $value);
}
$idList2 = array_diff_assoc($idList, array_unique($idList));
foreach ($idList2 as $key => $value) {
    $akey = array_search($value, $idList);
    echo '重复数组:';
    print_r($array[$akey]);
}