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;}}
Zalif
  • 125
  • 1
  • 12
  • Not saying this is the answer, but here's something for reference, in case this helps: http://stackoverflow.com/questions/19964353/array-diff-on-array-of-associative-arrays-in-php – Samuel Reid Oct 07 '15 at 16:48

3 Answers3

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.

William_Wilson
  • 1,244
  • 7
  • 16
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);
?>
Payer Ahammed
  • 887
  • 1
  • 5
  • 16
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.

Payer Ahammed
  • 887
  • 1
  • 5
  • 16