Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.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中精确匹配两个数组中的键和值_Php_Arrays - Fatal编程技术网

如何在php中精确匹配两个数组中的键和值

如何在php中精确匹配两个数组中的键和值,php,arrays,Php,Arrays,我有两个数组,我想精确匹配数组的键和值,其中一个数组是三维的或更多的 范例 $arr1 = ['status'=>true,'message'=>'data saved']; $arr2 = ['status'=>true,'message'=>'data saved']; 在这种情况下,数组返回1,但它们不相等 $arr1 = array("messagess"=>"data added","status" => true); $arr2 = array

我有两个数组,我想精确匹配数组的键和值,其中一个数组是三维的或更多的 范例

$arr1 = ['status'=>true,'message'=>'data saved']; 
$arr2 = ['status'=>true,'message'=>'data saved'];
在这种情况下,数组返回1,但它们不相等

$arr1 = array("messagess"=>"data added","status" => true);
$arr2 = array("status" => true,'message'=>'data has been added'); 
echo count(array_intersect_assoc($arr1,$arr2));
如果两者完全匹配,则预期值应为true,否则为false。我尝试了array_intersect()和其他方法,但失败了

请导游


提前感谢

您可以使用array_intersect_assoc()并计算结果

  echo count(array_intersect_assoc($arr1,$arr2));


如果计数中的数字与您想要的索引键的数量相同,请检查两个数组是否相同,否则您将获得匹配的键值数量

显示您尝试的代码。编写一些foreach循环并不是那么困难,因为解决这个数组应该是三维的或更高的——所以您可能有多维数组?函数是否也应该检查内部数组是否相等?要检查数组是否相等,仅当($arr1===$arr2){/**matched**/}但此$arr1=数组的计数(“messagess”=>“添加的数据”,“状态”=>true)时才执行$arr2=数组(“状态”=>true,“消息”=>“数据已添加”);返回1应该返回01是正确的。。在您的样品中,您正在寻找2。。因此,一个键值不匹配2.答案更新为简要说明。。有关更多信息,请参阅链接中的t php手册