Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 数组_diff不接受参数_Php_Arrays_Multidimensional Array_Array Difference - Fatal编程技术网

Php 数组_diff不接受参数

Php 数组_diff不接受参数,php,arrays,multidimensional-array,array-difference,Php,Arrays,Multidimensional Array,Array Difference,我正在尝试获取两个文件的差异: $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(

我正在尝试获取两个文件的差异:

$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);
lalala.json
的内容是:

`[{"name":"Tim Pearson","id":"17118"},{"name":"Ashley Danchen Chen","id":"504829084"},{"name":"Foisor Veronica","id":"100005485446135"}]`
alabala.json
的内容是

 `[{"name":"Tim Pearson","id":"17118"},{"name":"Foisor Veronica","id":"100005485446135"}]`
但是问题是,我得到了一个错误,因为内容不会被识别为数组(错误是
参数#1不是数组
)。如果执行
array\u diff($first,$second)
操作,则输出将是
$first
的内容,它是

Array ( [0] => [{"name":"Tim Pearson","id":"17118"},{"name":"Ashley Danchen Chen","id":"504829084"},{"name":"Foisor Veronica","id":"100005485446135"}] )

我应该如何处理这个问题?

您需要首先将JSON对象转换为数组,然后找出两个数组之间的差异。要将JSON字符串转换为数组,请使用
true
作为第二个参数:

$firstArray = json_decode($first, true);
如果省略第二个参数,$firstArray将是一个对象,即
stdClass
的实例

但首先需要将文件内容作为字符串,因此最好使用:

更新:
即使将JSON字符串正确地转换为数组,您仍然会遇到问题,因为它仅适用于一维数组,正如文档的
Notes
部分所述。要能够在多维数组中使用,请查看文档。

您的意思可能是

$first = json_decode(file_get_contents('lalala.json'), true);
$second = json_decode(file_get_contents('alabala.json'), true);

它会将字符串转换为数组,而不是对象(stdClass实例),但由于某种原因,现在的输出是一个空的arrayDid。在使用
json\u decode
之前,您可以从
file
切换到
file\u get\u contents
。有错误吗?顺便说一句,如果你从上面复制/粘贴我的代码,我刚刚意识到我拼错了名称…第一个数组的内容是
array([0]=>array([name]=>Tim Pearson[id]=>17118)[1]=>array([name]=>Ashley Danchen Chen[id]=>50489084)[2]=>array([name]=>Foissor Veronica[id]=>100005446135))
$first = json_decode(file_get_contents('lalala.json'), true);
$second = json_decode(file_get_contents('alabala.json'), true);