Hello,
I want to check difference between 2 arrays

I have two array with key value pair.
I want to check diff between these two. If there is diff then I will insert.
But in some cases, i have extra size in any one of arrays

or both array can be exact same , in which case i will not do anything.

Suppose I have one array as :

Array
(
    [0] => Array
        (
            [gslab_id] => GS-0001
            [college_name] => IIT ABC
            [degree_name] => B.Tech.
            [specialization_in] => Electrical
            [univercity] => IIT ABC
            [year_of_passing] => 0000-00-00
        )
    [1] => Array
        (
            [gslab_id] => GS-0001
            [college_name] => NA
            [degree_name] => M.S.
            [specialization_in] => Electrical
            [univercity] => Virginia Tech., USA
            [year_of_passing] => 0000-00-00
        )
    [2] => Array
        (
            [gslab_id] => GS-0001
            [college_name] => NA
            [degree_name] => Ph.D.
            [specialization_in] => Computer Science
            [univercity] => North Carolina State, USA
            [year_of_passing] => 0000-00-00
        )
)

and onther as :

Array
(
    [0] => Array
        (
            [gslab_id] => GS-0001
            [college_name] => IIT ABC
            [degree_name] => B.Tech.
            [specialization_in] => Electrical
            [univercity] => IIT ABC
            [year_of_passing] => 1983
        )
    [1] => Array
        (
            [gslab_id] => GS-0001
            [college_name] => NA
            [degree_name] => M.S.
            [specialization_in] => Electrical
            [univercity] => Virginia Tech., USA
            [year_of_passing] => 1985
        )
    [2] => Array
        (
            [gslab_id] => GS-0001
            [college_name] => NA
            [degree_name] => Ph.D.
            [specialization_in] => Computer Science
            [univercity] => ABCD
            [year_of_passing] => 1990
        )
     [3] => Array
        (
            [gslab_id] => GS-0001
            [college_name] => NA
            [degree_name] => M. Phil.
            [specialization_in] => Computer Science
            [univercity] => North Carolina State, USA
            [year_of_passing] => 1995
        )
)

In above case I have , I have diff between [2]nd array as well as new array is added at [3]

So how can I check in such situation .

I tried with function [got from stackoverflow ]

public function multi_diff($arr1,$arr2){
              $result = array();
              foreach ($arr1 as $k=>$v){
                if(!isset($arr2[$k])){
                  $result[$k] = $v;
                } else {
                  if(is_array($v) && is_array($arr2[$k])){
                    $diff = $this->multi_diff($v, $arr2[$k]);
                    if(!empty($diff))
                      $result[$k] = $diff;
                  }
                }
              }
            return $result;
        }

but is is not giving outout.
it return

Array
(
)
Inline Code Example Here

where there is diff even.

I tried with serialization diff, it gives me "there is diff " when there is not.

Totaly clueless now for this conditions.
Can anyone help me?
I was thinking of one solution: make only one array of diff.

But, in case difference between values , I want to update those perticular array . So i could not process of "delete -> and then insert"

Hi,

the function array_diff() will return empty:

  1. if the first array is smaller then the second;
  2. and if the elements in the first array range are the same of the second.

For example:

$a = [1,2];
$b = [1,2,3];

print_r(array_diff($a, $b);

Will return empty, instead:

$a = [1,2];
$b = [1,4,3];

print_r(array_diff($a, $b);

Will return 2. This:

$a = [1,2];
$b = [1,4,3];

print_r(array_diff($b, $a);

Will return 4 and 3, because in this case the size of the first array ($b) is larger. So if you compare the sizes you can decide which submit as array1 and which as array2. Does this help?

To compare I would serialize them to get a digest, for example:

<?php

    $a = [
        0 => ['a' => '1'],
        1 => ['a' => '1', 'b' => '2'],
    ];

    $b = [
        0 => ['a' => '1'],
        1 => ['a' => '1', 'b' => '3'],
        2 => ['a' => '1', 'b' => '2', 'c' => '3'],
    ];

    function generate_digest($array)
    {
        $str = serialize($array);
        return md5($str);
    }

    function my_diff($first, $second)
    {
        $n1 = count($first);
        $n2 = count($second);

        if($n1 >= $n2)
            return array_diff($first, $second);

        return array_diff($second, $first);
    }

    $first  = array_map('generate_digest', $a);
    $second = array_map('generate_digest', $b);
    $diff   = my_diff($first, $second);

    print_r($diff);

It's not a strong solution because is case-sensitive, and a simple extra space would make a difference. Anyway the function array_diff() will return the values preserving the keys, so the next step would be to get those arrays and start the other operations... it's here that I get lost: what are you trying to do exactly? It seems you're trying to keep updated two different sources. The order of the arrays is important? How to define which data is old? The source is a database table? And the destination is a database table?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.