Check differences of two array in PHP

1

I'm trying to check the difference of two arrays to insert or delete data in MySQL but the code I have fails a lot and the same is another way to make it more appropriate, but I do not know it.

The code of the comparator and the function to erase is this:

$Diferencia = array_udiff(
    $Todo,
    $EnFormulario,
    function($a, $b) { //compruebo las diferencias en ambos array para saber que borrar y que agregar
        if ($a['id'] == $b['id']) {
            return 0;
        } // Si es igual
        return -1;
    }
);

if (count($Diferencia) >= 1) {
    foreach($Diferencia as $BorrarCod) {
        $BorrarCodigos = Borrar_Datos(
            "Direcciones" ,
            "id",
            "".$BorrarCod["id"].""
        );
    }
} //borro lo que ya no exista y cierro el count

On the other hand I have two MySQL queries that generate the two arrays to compare what these would be:

$Todo = Consulta_Dinamica(
    "Array",
    "*",
    "Direcciones",
    "'que' = 'Codigo' AND 'idcosa' = '" . $_POST["art_nuevo"] . "' "
);

$EnFormulario = [];
foreach($UpTallicolor as $DUpColorTalli) {
    $EnFormulario[] = Consulta_Dinamica(
        "Simple",
        "*",
        "Direcciones",
        "'que' = 'Codigo' AND 'idcosa' = '" . $_POST["art_nuevo"] .
            "' AND '1'='" . $DUpColorTalli['tallas'] .
            "' AND '2'='" . $DUpColorTalli['color'] . "' "
    );
}

In $Todo I consult all the data and store them in an array. In the variable $EnFormulario what I do is a simple query and I store the result in that same variable (or so I think I do).

The data returned by the two arrays are these:

    $Todo 
Array
    (
        [0] => Array
            (
                [id] => 1112
                [que] => Codigo
                [idcosa] => 80
                [1] => 129
                [2] => 40
                [3] => 843446300048
                [4] => 
                [5] => 
                [6] => 
                [7] => 
                [8] => 
                [9] => 
                [10] => 
            )

        [1] => Array
            (
                [id] => 1113
                [que] => Codigo
                [idcosa] => 80
                [1] => 36
                [2] => 40
                [3] => 843446300049
                [4] => 
                [5] => 
                [6] => 
                [7] => 
                [8] => 
                [9] => 
                [10] => 
            )

        [2] => Array
            (
                [id] => 1114
                [que] => Codigo
                [idcosa] => 80
                [1] => 129
                [2] => 87
                [3] => 843446300050
                [4] => 
                [5] => 
                [6] => 
                [7] => 
                [8] => 
                [9] => 
                [10] => 
            )

        [3] => Array
            (
                [id] => 1115
                [que] => Codigo
                [idcosa] => 80
                [1] => 36
                [2] => 87
                [3] => 843446300051
                [4] => 
                [5] => 
                [6] => 
                [7] => 
                [8] => 
                [9] => 
                [10] => 
            )

    )

$Enformulario
Array
    (
        [0] => Array
            (
                [0] => 1112
                [id] => 1112
                [1] => 129
                [que] => Codigo
                [2] => 40
                [idcosa] => 80
                [3] => 843446300048
                [4] => 
                [5] => 
                [6] => 
                [7] => 
                [8] => 
                [9] => 
                [10] => 
                [11] => 
                [12] => 
            )

        [1] => Array
            (
                [0] => 1113
                [id] => 1113
                [1] => 36
                [que] => Codigo
                [2] => 40
                [idcosa] => 80
                [3] => 843446300049
                [4] => 
                [5] => 
                [6] => 
                [7] => 
                [8] => 
                [9] => 
                [10] => 
                [11] => 
                [12] => 
            )

I have tried to put a simple array_diff but it does not return any results even though between $ All and $ Enformulario have obvious changes ...

I really just need to delete what is in $ All that is not in $ Enformulario

    
asked by Killpe 05.05.2017 в 13:26
source

2 answers

1

In general, your idea works with the data you have provided, but the problem you are suffering is that you are calculating the difference in data between two matrices, when you want to find out what data from one matrix have ceased to exist in another , which are two different things.

Example:

  • A - > 1, 4, 5
  • B - > 2, 4, 7

Difference (those in A that are not in B and those in B that are not in A):

1, 5, 2, 7

What's in A that are not in B:

1, 5

One way to carry out the work would be with the following code:

<?php
$Todo = array(
    0 => array(
        'id' => 1112,
        'que' => 'Codigo',
        'idcosa' => 80,
        1 => 129,
        2 => 40,
        3 => 843446300048,
    ),
    1 => array(
        'id' => 1113,
        'que' => 'Codigo',
        'idcosa' => 80,
        1 => 36,
        2 => 40,
        3 => 843446300049,
    ),
    2 => array(
        'id' => 1114,
        'que' => 'Codigo',
        'idcosa' => 80,
        1 => 129,
        2 => 87,
        3 => 843446300050,
    ),
    3 => array(
        'id' => 1115,
        'que' => 'Codigo',
        'idcosa' => 80,
        1 => 36,
        2 => 87,
        3 => 843446300051,
    )
);

$EnFormulario = array(
    0 => array(
        0 => 1112,
        'id' => 1112,
        1 => 129,
        'que' => 'Codigo',
        2 => 40,
        'idcosa' => 80,
        3 => 843446300048,
    ),

    1 => array(
        0 => 1113,
        'id' => 1113,
        1 => 36,
        'que' => 'Codigo',
        2 => 40,
        'idcosa' => 80,
        3 => 843446300049,
    )
);

/* Generamos un índice de $EnFormulario */
$formulario = [];
foreach($EnFormulario as $clave => $valor) {
    if (isset($valor['id'])) {
        $formulario[$valor['id']] = true;
    }
}

foreach($Todo as $valor) {
    if (isset($valor['id'])) {
        /* Buscamos en el índice y si no existe el valor lo borramos */
        if (!isset($formulario[$valor['id']])) {
            echo "<p>Borrando {$valor['id']}</p>\n";
        }
    }
}

Ideally, you would generate the index directly from the data obtained and not making a loop to go through them.

One way to do it would be by modifying the data collection like this:

$Todo = Consulta_Dinamica(
    "Array",
    "*",
    "Direcciones",
    "'que' = 'Codigo' AND 'idcosa' = '" . $_POST["art_nuevo"] . "' "
);
/* Generamos directamente $formulario con su índice por 'id' */
$formulario = [];
foreach($UpTallicolor as $DUpColorTalli) {
    $EnFormulario = Consulta_Dinamica(
        "Simple",
        "*",
        "Direcciones",
        "'que' = 'Codigo' AND 'idcosa' = '" . $_POST["art_nuevo"] .
            "' AND '1'='" . $DUpColorTalli['tallas'] .
            "' AND '2'='" . $DUpColorTalli['color'] . "' "
    );
    if (isset($EnFormulario['id'])) {
        $formulario[$EnFormulario['id']] = $EnFormulario;
    }
}
/* Ya no hace falta hacer el bucle foreach que generaba los índices a partir de la matriz $EnFormulario */
foreach($Todo as $valor) {
    if (isset($valor['id'])) {
        /* Buscamos en el índice y si no existe el valor lo borramos */
        if (!isset($formulario[$valor['id']])) {
            echo "<p>Borrando {$valor['id']}</p>\n";
        }
    }
}

I hope you now get a better idea of how to generate the matrix with the results of the search using one of the fields of the result as an index.

    
answered by 08.05.2017 / 13:44
source
-3

To find the difference between two arrays with array_diff is enough.

link

If you use array_diff ($ array1, $ array2) it will return the elements of $ array1 that do not exist in $ array2

Greetings

    
answered by 22.05.2018 в 16:44