Dinesh
Dinesh

Reputation: 197

How to get difference between Associate arrays in PHP

I have an arrays like this

Array
(
    [original_data] => Array
        (
            [0] => Array
                (
                    [reference_id] => 122
                    [id] => 121
                    [reference_name] => Dinesh
                )

            [1] => Array
                (
                    [reference_id] => 123
                    [id] => 120
                    [reference_name] => Dinesh Test
                )

        )

    [edited_data] => Array
        (
            [0] => Array
                (
                    [reference_id] => 123
                    [id] => 120
                    [reference_name] => Dinesh Test2
                )

        )

)

I want to get the difference between this original_data and edited_data arrays. How to do that? I tried using array_diff. But, It didn't work correctly.

Upvotes: 1

Views: 91

Answers (4)

Balasuresh Asaithambi
Balasuresh Asaithambi

Reputation: 537

You can get the difference =>

$result = ref_array_diff($requestedData['edited_data'], $requestedData['data']);

print_r($result);

function ref_array_diff($arraya, $arrayb) {
    foreach ($arraya as $keya => $valuea) {
        if (in_array($valuea, $arrayb)) {
            unset($arraya[$keya]);
        }
    }
    return $arraya;
}

Upvotes: 1

Jainam Shah
Jainam Shah

Reputation: 201

These are multi-dimensional associative arrays, you can recursively do an array_diff_assoc like this: you can check this code here https://3v4l.org/ovaQB:

function array_diff_assoc_recursive($array1, $array2)
{
    foreach($array1 as $key => $value)
    {
        if(is_array($value))
        {
            if(!isset($array2[$key]))
            {
                $difference[$key] = $value;
            }
            elseif(!is_array($array2[$key]))
            {
                $difference[$key] = $value;
            }
            else
            {
                $new_diff = array_diff_assoc_recursive($value, $array2[$key]);
                if($new_diff != FALSE)
                {
                    $difference[$key] = $new_diff;
                }
            }
        }
        elseif(!isset($array2[$key]) || $array2[$key] != $value)
        {
            $difference[$key] = $value;
        }
    }
    return !isset($difference) ? 0 : $difference;
}

$a=["original_data"=>[
                [
                    'reference_id' => 122,
                    'id' => 121,
                    'reference_name' => 'Balasuresh'
                ],[
                    'reference_id' => 123,
                    'id' => 120,
                    'reference_name' => 'Balasuresh'
                ]

        ],

    'edited_data' => [[
                    'reference_id' => 123,
                    'id' => 120,
                    'reference_name' => 'Balasuresh Test'
                ]]

];

print_r(array_diff_assoc_recursive($a['edited_data'], $a['original_data']));

output:

Array
(
    [0] => Array
        (
            [reference_id] => 123
            [id] => 120
            [reference_name] => Balasuresh Test
        )

)

Upvotes: 0

Mr Glass
Mr Glass

Reputation: 1306

This should work.

<?php

// your data
$data = array (
    'original_data' => array
        ('0' => array
            (
                'reference_id' => 122,
                'id' => 121,
                'reference_name' => 'Balasuresh,'
            ),

         '1' => array
            (
                'reference_id' => 123,
                'id' => 120,
                'reference_name' => 'Balasuresh',
            ),
        ),

    'edited_data' => array
        ('0' => array
            (
               'reference_id' => 123,
               'id' => 120,
               'reference_name' => 'Balasuresh Test',
            ),
        )

) ;


// let's get our reference_ids
$originalData = array() ;
foreach($data['original_data'] as $entry) {
    $originalData[$entry['reference_id']] = $entry ;
}

// let's compare our edited data to our original data
$editedData = array() ;
foreach($data['edited_data'] as $editedEntry) {
    if (isset($originalData[$editedEntry['reference_id']])) {
        // for readability
        $compare = $originalData[$editedEntry['reference_id']] ;
        foreach($editedEntry as $key=>$value) { 
            if ($compare[$key] != $value) {         
                $editedData[$editedEntry['reference_id']][$key] = $value ; 
            }
        }
    }
}

$editedData now contains your differences

Upvotes: 0

Shardj
Shardj

Reputation: 1969

If you use array_map on both original data and edited data to get an array of original_data id values and edited_data id values. You can then use array_diff with no problem. Using the resulting list of id's which are in one but not the other you can lookup the original value using the array mapped values index key on the original arrays.

I'm making the assumption here that your id key is a unique identifier for each value in the data arrays. If this isn't the case you'll need to do some nested loops to compare deep values

Upvotes: 0

Related Questions