[英]find difference between two associative multidimensional arrays
I have two associative multidimensional arrays, now I want to get values which are not present in the second array (compare by both key name and type). 我有两个关联的多维数组,现在我想获取第二个数组中不存在的值(按键名和类型进行比较)。
$sql = "select * from `medicine`;";
$result = mysqli_query($con, $sql);
$response = array();
while ($row = mysqli_fetch_array($result)) {
array_push($response, array("name" => ucfirst($row['name']), "type" => $row['type']));
}
Output : 输出:
Array ( [0] => Array ( [name] => A2 [type] => syringe ) [1] => Array ( [name] => A3 [type] => syringe ) [2] => Array ( [name] => A3 [type] => capsule ) [3] => Array ( [name] => A4 [type] => syringe )[4] => Array ( [name] => A5 [type] => a ) [5] => Array ( [name] => A6 [type] => capsule ) )
$s = "select * from `doctor_medicine` where `email` = '$email';";
$res = mysqli_query($con, $s);
$resp = array();
while ($row = mysqli_fetch_array($res)) {
array_push($resp, array("name" => ucfirst($row['medicine_name']),"type" => $row['type']));
}
Output : 输出:
Array ( [0] => Array ( [name] => A2 [type] => syringe ) [1] => Array ( [name] => A3 [type] => capsule ) [2] => Array ( [name] => A4 [type] => syringe ) )
I don't know how to find difference Result : 我不知道如何找到不同的结果:
Array ( [0] => Array ( [name] => A3 [type] => capsule ) [4] => Array ( [name] => A5 [type] => a ) [5] => Array ( [name] => A6 [type] => capsule ) )
You can use array_diff_assoc php function to find difference or value that is not present in array. 您可以使用array_diff_assoc php函数查找数组中不存在的差异或值。 Example: 例:
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_assoc($array1, $array2);
print_r($result);
Example output: 输出示例:
Array
(
[b] => brown
[c] => blue
[0] => red
)
There is a function array_diff_assoc()
for example: 例如,有一个函数array_diff_assoc()
:
<?php
$a = array(
"who" => "you",
"what" => "thing",
"where" => "place",
"when" => "hour"
);
$b = array(
"when" => "time",
"where" => "place",
"who" => "you",
"what" => "thing"
);
$c = array_diff_assoc($a, $b);
print_r ($c);
?>
You can try above code here 您可以在这里尝试上面的代码
And you can use array_intersect()
for find resemblance.for example: 您可以使用array_intersect()
查找相似之处,例如:
<?php
$a = array(
"who" => "you",
"what" => "thing",
"where" => "place",
"when" => "hour"
);
$b = array(
"when" => "time",
"where" => "place",
"who" => "you",
"what" => "thing"
);
$c = array_intersect($a, $b);
print_r ($c);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.