1

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.

3
  • what output you want? please specify. Commented Jun 15, 2018 at 9:45
  • @JainamShah the output needs to be [0] => Array ( [reference_id] => 123 [id] => 120 [reference_name] => Balasuresh Test ) Commented Jun 15, 2018 at 9:49
  • Currently that's is just: $data['edited_data']. What do you mean by difference? Commented Jun 15, 2018 at 11:41

4 Answers 4

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

0

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
        )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.