webdeveloper123 Posted May 6, 2022 Share Posted May 6, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/314762-using-array_diff-on-2-arrays/ Share on other sites More sharing options...
requinix Posted May 6, 2022 Share Posted May 6, 2022 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) Quote Link to comment https://forums.phpfreaks.com/topic/314762-using-array_diff-on-2-arrays/#findComment-1595968 Share on other sites More sharing options...
webdeveloper123 Posted May 6, 2022 Author Share Posted May 6, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/314762-using-array_diff-on-2-arrays/#findComment-1595971 Share on other sites More sharing options...
Solution gizmola Posted May 6, 2022 Solution Share Posted May 6, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/314762-using-array_diff-on-2-arrays/#findComment-1595985 Share on other sites More sharing options...
webdeveloper123 Posted May 7, 2022 Author Share Posted May 7, 2022 thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/314762-using-array_diff-on-2-arrays/#findComment-1596008 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.