[英]How to get the difference between two multidimensional arrays of associative arrays in PHP?
[英]find difference between two associative multidimensional arrays
我有兩個關聯的多維數組,現在我想獲取第二個數組中不存在的值(按鍵名和類型進行比較)。
$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']));
}
輸出:
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']));
}
輸出:
Array ( [0] => Array ( [name] => A2 [type] => syringe ) [1] => Array ( [name] => A3 [type] => capsule ) [2] => Array ( [name] => A4 [type] => syringe ) )
我不知道如何找到不同的結果:
Array ( [0] => Array ( [name] => A3 [type] => capsule ) [4] => Array ( [name] => A5 [type] => a ) [5] => Array ( [name] => A6 [type] => capsule ) )
您可以使用array_diff_assoc php函數查找數組中不存在的差異或值。 例:
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_assoc($array1, $array2);
print_r($result);
輸出示例:
Array
(
[b] => brown
[c] => blue
[0] => red
)
例如,有一個函數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);
?>
您可以在這里嘗試上面的代碼
您可以使用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.