How do I use array_udiff() correctly?

本小妞迷上赌 提交于 2019-12-13 00:27:08

问题


I have two multidimensional arrays that both look something like this:

Array
(
    [0] => Array (
         'id' => 3,
         'other' => 'some string',
         'timestamp' => 2000-01-01 00:00:00
    ),

    [1] => Array (
         'id' => 6,
         'other' => 'another string',
         'timestamp' => 1835-01-01 00:00:00
    )
)

I'm trying to find a way to figure out which elements show up in one array ($b), but not the other ($a) and if there are any elements with changed values. If $a is:

Array
(
    [0] => Array (
         'id' => 3,
         'other' => 'some string',
         'timestamp' => 2000-01-01 00:00:00
    )
)

and $b is:

Array
(
    [0] => Array (
         'id' => 3,
         'other' => 'some string',
         'timestamp' => 2000-01-01 12:12:12
    ),

    [1] => Array (
         'id' => 4,
         'other' => 'some string',
         'timestamp' => 1900-01-01 01:12:23
    )
)

Then the function should return:

Array
(
    [0] => Array (
         'id' => 3,
         'other' => 'some string',
         'timestamp' => 2000-01-01 12:12:12
    ),

    [1] => Array (
         'id' => 4,
         'other' => 'some string',
         'timestamp' => 1900-01-01 01:12:23
    )
)

because the element with id = 3 has been changed (the timestamp field) and the element with id = 4 is new and doesn't appear in the other array.

I've been trying to do this with array_udiff, but I still don't know how it works (it seems to sort both arrays first, but then how does it do comparison?). Is array_udiff the proper method or should I write a custom function?


回答1:


You can use array_udiff and define your own comparison callback. I assume that both arrays has exactly the same structure.

You can define your own callback function as follow:

int comparison(Array $a, Array $b){
    if ($a['id']==$b['id'] && $a['other']==$b['other'] && $a['timestamp']==$b['timestamp']){
        return 0
    }else{
        return -1
    }
}

The callback function must return a negative integer if first argument is less than the second; a positive number if it's bigger; or 0 if it's equal. Then, you can return any number different to 0 to indicate that the arguments are different and 0 if they are the equal.

Finally, you should call array_udiffas follow:

array_udiff($a, $b, 'comparison')

And you will get a list of the elements of $a which are not, or are different in $b.

Note that if you want to compare 2 array when one of then has more elements than the other, you should pass as first argument the array with the new elements.




回答2:


The return for array_udiff function "data_compare_func" is some function that you define but it must return an integer less than, equal to, or greater than zero so its probably not the right function for your needs. A custom function like this should give you what you need:

// this function loops through both arrays to find a match in the other array
// it will skip entry comparisons when it goes through $arr2 because you already did it the first time
function find_diff($arr1, $arr2) {
    $ret = array();

    // we need to do two loops to find missing entries from both arrays
    $ret = do_loop($arr1, $arr2, $ret, true);
    $ret = do_loop($arr2, $arr1, $ret, false);
    return $ret;
}

// this function does the looping though $arr1 to compare it to entries in $arr2
// you can skip entry comparison if $compare_entries is false
function do_loop($arr1, $arr2, $ret, $compare_entries = true) {
    //look through all of $arr1 for same element in $arr2 based on $id
    for ($i=0;$i<count($arr1);$i++) {
        $id = $arr1[$i]['id'];
        $found = false;

        for ($j=0;$j<count($arr2);$j++) {
            // id match found
            if ($id == $arr2[$j]['id']) {
                $found = true;
                // only compare entries if you need to
                if ($compare_entries) {
                    //check if other field is different
                    if (strcmp($arr1[$i]['other'],$arr2[$j]['other']) != 0) {
                        $ret = add_to_ret($arr1[$i], $ret);
                        break;
                    }
                    //check if timestamp field is different
                    if (strcmp($arr1[$i]['timestamp'],$arr2[$j]['timestamp']) != 0) {
                        $ret = add_to_ret($arr1[$i], $ret);
                        break;
                    }
                } else {
                    break;
                }
            }
        }

        // entry from $arr1[$i] was not found in $arr2
        if (!$found) {
            $ret = add_to_ret($arr1[$i], $ret);
        }
    }
    return $ret;
}


//this function only adds the new entry to $ret if it's ID isn't already in $ret
function add_to_ret($entry, $ret) {

    $id = $entry['id'];

    for ($i=0;$i<count($ret);$i++) {
        if ($id == $ret[$i]['id']) {
            //skip adding, its already in there
            return $ret;
        }
    }
    //add it in
    $ret[] = $entry;
    return $ret;
}


来源:https://stackoverflow.com/questions/18341520/how-do-i-use-array-udiff-correctly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!