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