2

I'm having trouble with array_diff_assoc because the result is not right. Im'm getting key [3] as a result in $diff but it exists in both $c1 and $c2 so the result in $diff doesn't makes sense since I'm using array_diff_assoc(). I should be getting key[0] from $c2 along with the other two keys [1] and [2] from $diff which are correct.

Query1 results in this array $c2

Array([0] => Array
        ([PROCESSO] => 1614436
         [DATAMSG] => 2015-04-27 22
         [CATEGORIA_DESC] => ECG_HR                        
         [VALOR] => 135
         [CRITICO] => 1)
    [1] => Array
        ([PROCESSO] => 1614436
         [DATAMSG] => 2015-04-27 23
         [CATEGORIA_DESC] => ECG_HR                        
         [VALOR] => 121
         [CRITICO] => 1)
    [2] => Array
        ([PROCESSO] => 1614436
         [DATAMSG] => 2015-04-27 23
         [CATEGORIA_DESC] => ECG_HR                        
         [VALOR] => 123
         [CRITICO] => 1)
    [3] => Array
        ([PROCESSO] => 1614436
         [DATAMSG] => 2015-04-27 23
         [CATEGORIA_DESC] => ECG_HR                        
         [VALOR] => 125
         [CRITICO] => 1))

Query 2 results in this array $c1

Array([0] => Array
        ([PROCESSO] => 1614436
         [DATAMSG] => 2015-04-27 23
         [CATEGORIA_DESC] => ECG_HR                        
         [VALOR] => 125
         [CRITICO] => 1))

Then I use $diff=array_diff_assoc($c2,$c1); and I get the results below in array $diff.

Array([1] => Array
        ([PROCESSO] => 1614436
         [DATAMSG] => 2015-04-27 23
         [CATEGORIA_DESC] => ECG_HR                        
         [VALOR] => 121
         [CRITICO] => 1)
    [2] => Array
        ([PROCESSO] => 1614436
         [DATAMSG] => 2015-04-27 23
         [CATEGORIA_DESC] => ECG_HR                        
         [VALOR] => 123
         [CRITICO] => 1)
    [3] => Array
        ([PROCESSO] => 1614436
         [DATAMSG] => 2015-04-27 23
         [CATEGORIA_DESC] => ECG_HR                        
         [VALOR] => 125
         [CRITICO] => 1))

EDIT: I tried using $diff=array_udiff_assoc($c2,$c1, array("cr", "comp_func_cr"));with the below function as in php.net example but it's returning the same thing as with array_diff_assoc.

class cr {
    private $priv_member;
    function cr($val)
    {$this->priv_member = $val;}
    static function comp_func_cr($c2, $c1)
    {if ($c2->priv_member === $c1->priv_member) return 0;
        return ($c2->priv_member > $c1->priv_member)? 1:-1;}}
1

3 Answers 3

1

Try the following

<?php
foreach($c2 as $key=>$v1){
    if(in_array($v1, $c1)){
      unset($c2[$key]);
    }
}
print_r($c2);
?>

or you can make a function and use it example:

<?php
function array_defference($a1,$a2){
            if(sizeof($a1) >= sizeof($a2)){
                $large = $a1;
                $small = $a2;
            }else{
                $large = $a2;
                $small = $a1;
            }

            foreach($large as $key=>$v1){
                if(in_array($v1, $small)){
                    unset($large[$key]);
                }
            }
            return $large;

        }
        $reuslt = array_defference($c2,$c1); 
        print_r($reuslt);
?>
Sign up to request clarification or add additional context in comments.

3 Comments

This will only work if all of $c1's items are in $c2 and $c2 is the larger array... It answers this exact question, but is not very reusable.
Probably it reusable now. Thanks @William_Wilson for your feedback
Yes it works fine thanks. Since $c2 is always bigger or equal it is fine I believe.
1

To expand the previous answer:

function array2D_diff( $c1, $c2 )
{
    $c3 = Array();
    foreach($c2 as $key=>$v)
    {
        if(!in_array($v, $c1))
        {
            $c3[] = $v;
        }
    }

    foreach($c1 as $key=>$v)
    {
        if(!in_array($v, $c2))
        {
            $c3[] = $v;
        }
    }

    return $c3;
}

print_r(array2D_diff( $c2, $c1 ));

This will work regardless of the order of passing $c2 and $c1.

3 Comments

I tried that as you said but didn't work, I edited my post with my attempt.
How are you calling array_udiff_assoc?
Sorry, the array_udiff_assoc is for objects, not 2D arrays.
0

array_diff_assoc(array1,array2) Compares array1 against array2 and returns the difference. Unlike array_diff() the array keys are also used in the comparison. It's actually compare with your array key so result is like your mansion.In your case you need to use this array_udiff_assoc($c2,$c1). Computes the difference of arrays with additional index check, compares data by a callback function.

1 Comment

Tried but no luck, check my edit to see if I did it the right way.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.