php - Array_diff not taking in arguments -


i trying difference of 2 files:

$first = file('lalala.json'); $second = file('alabala.json'); //print_r($first); //print_r($second); $first_result = array_diff($first[0], $second[0]); //$second_result = array_diff($second, $first); print_r($first_result); //print_r($second_result); 

the content of lalala.json is:

`[{"name":"tim pearson","id":"17118"},{"name":"ashley danchen chen","id":"504829084"},{"name":"foisor veronica","id":"100005485446135"}]` 

while content of alabala.json

 `[{"name":"tim pearson","id":"17118"},{"name":"foisor veronica","id":"100005485446135"}]` 

however problem error, because content not recognised array (the error argument #1 not array). if array_diff($first, $second) output content of $first

array ( [0] => [{"name":"tim pearson","id":"17118"},{"name":"ashley danchen chen","id":"504829084"},{"name":"foisor veronica","id":"100005485446135"}] ) 

how should handle this?

you need convert json objects arrays first , find difference between 2 arrays. convert json string array use json_decode() true second parameter:

$firstarray = json_decode($first, true); 

if leave second parameter out, $firstarray object, instance of stdclass.

but first you'd need content of file string, better use file_get_contents():

$first = file_get_contents('lalala.json'); 

update:
when you've converted json strings array, you'll still have problem, array_diff() works 1 dimensional arrays, it's mentioned in notes section of documentation. able use in on multidimensional arrays, have @ this comment documentation.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -