Jump to content

using array_diff on 2 arrays


Go to solution Solved by gizmola,

Recommended Posts

Hi,

I have the following arrays:

$table = array ( 
            0 => array ( 
                'fname' => 'Peter', 
                'lname' => 'Smith', 
                'age' => '37'
                ), 
            1 => array ( 
                'fname' => 'Paul', 
                'lname' => 'Hartley', 
                'age' => '48'
                ), 
            2 => array ( 
                'fname' => 'Mary', 
                'lname' => 'Baker', 
                'age' => '42'
                ), 
            3 => array ( 
                'fname' => 'Jane', 
                'lname' => 'Doe', 
                'age' => '51'
              
                )
            );


$newdata = array ( 
            4 => array ( 
                'fname' => 'Paul', 
                'lname' => 'Hartley', 
                'age' => '48'
                ), 
            5 => array ( 
                'fname' => 'Mary', 
                'lname' => 'Baker', 
                'age' => '42'
                ), 
            6 => array ( 
                'fname' => 'Jane', 
                'lname' => 'Doe', 
                'age' => '51'
                ), 
            7 => array ( 
                'fname' => 'Toney', 
                'lname' => 'Brentford', 
                'age' => '25'
              
                )
            );

I am trying to use array_diff on the 2 arrays and based on my understanding only Peter Smith 37 should be returned. I am using this code:

$result = array_diff($table, $newdata);
print_r($result);

But I am getting same errors: (8 times)

 Array to string conversion in

Array ( )

Note the Array ( ) only shows once in the error.

On php.net it says array_diff is used in 1 dimensional arrays but you can: Of course you can check deeper dimensions by using:

array_diff($array1[0], $array2[0]);

So I did:

$result = array_diff($table[0], $newdata[0]);

From my understanding the number in the square bracket refers to the key in the array but I get this error:

Notice: Undefined offset: 0 in

Warning: array_diff(): Expected parameter 2 to be an array, null given 

I also used:

$result = array_diff($table[fname], $newdata[fname]);

But I get a whole host of errors

So I noticed that the examples in array_diff were all using the same key for multiple values, So Instead I tried array_diff_key:

$result = array_diff_key($table, $newdata);
print_r($result);

And I do get output, but not the expected output. This is the print_r($result) output

Array ( [0] => Array ( [fname] => Peter [lname] => Smith [age] => 37 ) [1] => Array ( [fname] => Paul [lname] => Hartley [age] => 48 ) [2] => Array ( [fname] => Mary [lname] => Baker [age] => 42 ) [3] => Array ( [fname] => Jane [lname] => Doe [age] => 51 ) )

but surely only Peter Smith 37 Should be the only output. Can anyone see where I'm going wrong?

Many thanks

Link to comment
https://forums.phpfreaks.com/topic/314762-using-array_diff-on-2-arrays/
Share on other sites

array_diff will try to compare values like they were scalar values. For example, as strings. Thus the warnings about converting an array to a string. What you tried after that was throwing random code at the problem and hoping it would go away, which is inefficient and rarely ever works.

If you want a simple comparison between the values then use array_udiff() with a function that does a simple comparison - such as by using the <=> operator.

array_udiff($table, $newdata, fn($a, $b) => $a <=> $b)

 

sorry I didn't understand your code...is it missing function before fn? I tried it and then I tried:

print_r(fn($table, $newdata));

But I get error:

Parse error: syntax error, unexpected ')', expecting => (T_DOUBLE_ARROW) in

 

  • Solution

He literally gave you code that works, only missing a semicolon:

$result = array_udiff($table, $newdata, fn($a, $b) => $a <=> $b);

var_dump($result);

If you are familiar with Javascript ES6, it is a similar type of anonymous function shorthand, equivalent to this:

$result = array_udiff($table, $newdata, function($a, $b) { return $a <=> $b; });

You need to have a current supported PHP version ( >= 7.4) although the syntax I showed works with any version 7.x or >

The "arrow function" version was introduced with php 7.4

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.