0

i need get value from 2 arrays...

First Array:

Array ( [0] => Array ( [id] => 1 [nombre_area] => biblioteca ) 
    [1] => Array ( [id] => 2 [nombre_area] => enfermeria ) 
    [2] => Array ( [id] => 3 [nombre_area] => talleres y laboratorios ) ) 

Second Array:

Array ( [0] => Array ( [0] => 1 [1] => biblioteca ) 
        [1] => Array ( [0] => 3 [1] => talleres y laboratorios ) )

i need get the difference:

Array ( [0] => Array ( [id] => 2 [nombre_area] => enfermeria )

How can i do that ?

Carlos Aviles
  • 171
  • 1
  • 1
  • 6

2 Answers2

0

You can try this one:

    $array1 =Array (Array ( 'id' => 1, 'nombre_area' => 'biblioteca' ),Array ( 'id' => 2, 'nombre_area' => 'enfermeria' ),Array ( 'id' => 3 ,'nombre_area' => 'talleres y laboratorios' ) );
    $array2 = Array (Array (1,'biblioteca' ), Array(3,'talleres y laboratorios' ));
    $IDs = array_map(function($arr2){return $arr2[0];},$array2);
    $result = array();
    foreach($array1 as $arr1){
        if(!in_array($arr1['id'],$IDs)) $result[] = $arr1; //compare id
    }
    print_r($result);
Lewis
  • 14,132
  • 12
  • 66
  • 87
0

You are not operating on associative arrays at top level. You have two numeric arrays containing nested arrays. One of those contains associative arrays, the other one numeric arrays. First you could bring it in a normalized form, e.g. by $normalized = array_map( function($ar) { return array_values($ar); }, $array1 ); to the numeric form.

However, then you have two structures of the same form, but array_diff() won't perform a deep inspection. It will only compare a string representation of the elements at the first level. So you won't have another choice than recursively iterate the array, e.g. with help of the function array_walk_recursive().

Pinke Helga
  • 6,378
  • 2
  • 22
  • 42