我有以下腳本,用于檢查/匹配兩個陣列之間的專案。如果第二個陣列包含不同的專案,則回傳false;
$requiredString = [
"string1",
"string2",
"string3",
"string4",
"string5"
];
$receivedString = [
"string1",
"string2",
"string3",
"string4",
"testString7"
];
foreach ($receivedString as $key => $value) {
if (!in_array($value, $requiredString)) {
return false;
}
}
return true;
該腳本運行良好,但我想重構該腳本。IE 縮短或減少執行時間。
有沒有可能重構腳本的方法?
uj5u.com熱心網友回復:
您可以使用 array_diff 函式:
return empty(array_diff($requiredString, $receivedString));
uj5u.com熱心網友回復:
試試這個:
<?php
$arr1 = [
"string1",
"string2",
"string3",
"string4",
"string5"
];
$arr2 = [
"string1",
"string2",
"string3",
"string4",
"testString7"
];
// Sort the array elements
sort($arr1);
sort($arr2);
// Check for equality
if ($arr1 == $arr2)
echo "Both arrays are same\n";
else
echo "Both arrays are not same\n";
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/396877.html
標籤:php
上一篇:單擊時如何在模態按鈕上獲取行ID
