Find the differences between arrays of type index = array in PHP

1

I need to find the differences between two arrays with the following structure:

array([0] => array([ciudad] => "Londres" [pais] => "Inglaterra") [1] => array([ciudad] => "París" [pais] => "Francia")... [n] => array([ciudad] => "Roma" [pais] => "Italia"))

When trying to apply array_diff I get an empty array even though there are pairs (city, country) in the first array that are not in the second; I think the problem is that the function is not able to enter the internal arrays and sees them all the same.

I am trying to make a more elaborate comparison through an if inside a for loop but it is not giving good results either:

$localizaciones_distintas = array();

for ($i = 0; $i < count($localizaciones_bd); $i++) 
{ 
    if ($localizaciones[$i] != $localizaciones_bd[$i]) array_push($localizaciones_distintas, $localizaciones[$i]);
}

Can you think of how to solve it?

Thanks in advance.

    
asked by Optigan 08.12.2018 в 03:33
source

2 answers

1

I have found a solution:

$localizaciones_distintas = array();

for ($i = 0; $i < count($localizaciones); $i++) 
{
    if (!in_array($localizaciones[$i], $localizaciones_bd)) array_push($localizaciones_distintas, $localizaciones[$i]);
}
    
answered by 08.12.2018 в 04:46
0

You can use the function array_diff_key

array_diff_key($array1, $array2);

Reference:
array_diff_key

    
answered by 08.12.2018 в 05:04