phparraysmultidimensional-arrayfilterarray-difference

Get the difference between two 2d arrays by a shared column


I have two arrays. $array_advise and $array_oab, respectively.

Array ( [0] => Array ([oab] => 226015 ) ... )

Array( [0] => Array([oab]=> 289240 )... )

The first one is the bigger array with the complete data. The second one is smaller. What I want is what is in $array_oab and is not in the $array_advise.

I tried with array_diff(), but it returns an empty array. What I'm doing wrong ?

Edit

  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));

I edited with my code, the output is a empty array.


Solution

  • array_diff() works with single-dimension arrays only. You have to use array_udiff(). Something like this:

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