Problem with array_diff

0

I'm having problems with the php array_diff_assoc function, it gives me the error

  

" Notice : Array to string conversion in"

The code is as follows:

static public function ctrMostrarConceptosLiquidarDistintos($item, $valor, $datos){

    $tabla = "Concepto";

    $listaConceptos = json_decode($datos, true);

    $nuevaLista = array();

    $nuevosValores = array();

    foreach ($listaConceptos as $key => $value) {

        if ($value["Fijo"] == "N") {

            $dtConceptos = array("ConceptoID" => $value["ConceptoID"],
                                "Descripcion" => $value["Descripcion"]);

            array_push($nuevaLista, $dtConceptos);
        }
    }

    $traerConceptos = ModeloPayment::mdlMostrarConceptosLiquidar($tabla, $item, $valor);

    foreach ($traerConceptos as $key2 => $value2) {

        $dtConceptos = array("ConceptoID" => $value2["ConceptoID"],
                             "Descripcion" => $value2["Descripcion"]);

        array_push($nuevosValores, $dtConceptos);
    }

    $felec = array_diff_assoc($nuevosValores, $nuevaLista);

    return $felec;
}

When the array $ listConcepts is empty, the array_diff_assoc function works and it returns an array, but when the array $ listConcepts has a value it gives me that error, which could be wrong?

    
asked by Felix Serrisuela 07.08.2018 в 10:17
source

2 answers

2

The problem is that you are working with multidimensional arrays (the elements of your arrays are also arrays), but array_diff_assoc only works with one-dimensional arrays (or a dimension in multidimensional arrays) as specified in a note from the PHP documentation :

  

This function only checks one dimension of an n-dimensional array. Of course, arrays of more dimensions can be checked using, for example, array_diff_assoc($array1[0], $array2[0]); .

On the same page, users offer different functions to solve this problem and compare all the dimensions and not just one (the basic idea on which they rely is to have a loop that executes the function for each dimension).

I leave here the one of Giosh , which is the most voted version:

<?php
function array_diff_assoc_recursive($array1, $array2) {
  $difference=array();
  foreach($array1 as $key => $value) {
    if( is_array($value) ) {
      if( !isset($array2[$key]) || !is_array($array2[$key]) ) {
        $difference[$key] = $value;
      } else {
        $new_diff = array_diff_assoc_recursive($value, $array2[$key]);
        if( !empty($new_diff) )
          $difference[$key] = $new_diff;
      }
    } else if( !array_key_exists($key,$array2) || $array2[$key] !== $value ) {
      $difference[$key] = $value;
    }
  }
  return $difference;
}
?>
    
answered by 07.08.2018 в 11:40
0

Develop this function that I think meets the criteria requested, is optimal and can be implemented generically.

<?php
$array1 = [array("id" => 1, "nombre" => "Juan", "apellido"=>"Ramirez"), 
array("id" => 2, "nombre" => "Micaela", "apellido"=>"Rodriguez"),
array("id" => 3, "nombre" => "Pedro", "apellido"=>"Martinez"),
array("id" => 4, "nombre" => "Ernesto", "apellido"=>"Perez")];

$array2 = [array("id" => 9, "nombre" => "Carlos", "apellido"=>"Suarez"), 
array("id" => 2, "nombre" => "Micaela", "apellido"=>"Rodriguez"),
array("id" => 5, "nombre" => "Agustin", "apellido"=>"Reinos"),
array("id" => 4, "nombre" => "Ernesto", "apellido"=>"Perez")];

$resultado = array_diff_multi($array1, $array2, "comparator_default");

print_r($resultado);

function array_diff_multi($a1, $a2, $comparator)
{
    if(!is_array($a1) ||!is_array($a2))
        return $a1;
    $retorno = Array();
    $existe = 0;
    foreach($a1 as $element)
    {
        $existe = 0;
        foreach($a2 as $element2)
        {
            if($comparator($element, $element2))
            {
                $existe = 1;
                break; // Detengo la ejecucion al encontrar al menos una coincidencia.
            }
        }
        if($existe == 0)
            array_push($retorno, $element);
    }
    return $retorno;
}

function comparator_default($a, $b)
{
    if(!is_array($a) || !is_array($b))
        return FALSE;
    if(!isset($a['id']) || !isset($a['nombre']) || !isset($a['apellido']))
        return FALSE;
    if(!isset($b['id']) || !isset($b['nombre']) || !isset($b['apellido']))
        return FALSE;
    if($a['id'] == $b['id'])
        if($a['nombre'] == $b['nombre'])
            if($a['apellido'] == $b['apellido'])
                return TRUE;
    return FALSE;
}
?>

I tried it only with the batch of the example, but I think it should work for any type of array. Greetings.

    
answered by 08.08.2018 в 01:25