试图在php中获取两个数组之间的差异

时间:2014-02-06 13:34:47

标签: php arrays

我有两个数组。分别为$array_advise$array_oab

  

数组([0] =>数组([oab] => 226015)...)

     

数组([0] =>数组([oab] => 289240)...)

第一个是包含完整数据的更大数组。第二个更小。我想要的是$array_oab中的内容,而不是the $array_advise

我尝试使用array_diff(),但它返回一个空数组。我做错了什么?

修改

  foreach ($lista_advise as $value_advise) {
    $array_advise[] = array("oab"=>trim($value_advise["OABNumero"]));
    $array_advise_nome[] = array("vinculo"=>$value_advise["nomeVinculo"]);
   // print_r($array_advise_nome);
  }              
    foreach ($corporativos->lista as $value){     
      $input = $value["info_adicionais"];
      $input = utf8_decode($input);
      $input = trim($input);
      $data = json_decode($input,true);          
      if ($data["andamentos"] != NULL) {
        $advogados = explode(";",$data["andamentos"]);
        foreach ($advogados as $advogado) {
          $dados = explode(",",$advogado);
          $oab = explode("/",$dados[1]);
          //  print_r(strtoupper(removerAcento($array_adv[0]["nome"])));  
          $array_adv[] = array("cliente" => $value["nome"], "clienteid" => $value["clienteId"] ,"nome"=>$dados[0], "oab"=>$oab[0], "estados"=>$oab[1]);  
          $array_oab[] = array("oab" => trim($oab[0]));
        }
      }
    }      
    print_r(array_diff($array_oab, $array_advise));

我用我的代码编辑,输出是一个空数组。

2 个答案:

答案 0 :(得分:2)

array_diff()仅适用于单维数组。您必须使用array_udiff()。 像这样:

function cmp($a, $b) {
return $a['oab'] == $b['oab']? 0 : 1;
}

array_udiff($array_oab, $array_advise, 'cmp');

答案 1 :(得分:0)

array_diff

<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);
?>