![](/img/trans.png)
[英]How can I compare two images for similarities (Not exact matches with MD5)?
[英]How to compare two arrays for similarities?
我正在嘗試比較兩個數組。 這兩個陣列均包含托運人的ID。 我需要比較這兩個數組,並且每次找到匹配項時,都需要執行sql查詢以提取電子郵件以獲取相應的ID。 這是我當前的代碼:
$orderid = mysql_query("SELECT MAX(orders_id) FROM orders") or die(mysql_error());
$orderid = mysql_fetch_row($orderid);
$purchasedProductId = mysql_query('SELECT products_id FROM orders_products WHERE orders_id = "' . $orderid[0] . '"');
$DSID = mysql_query("SELECT id FROM drop_shippers");
$DSIDarray = Array();
while ($row = mysql_fetch_array($purchasedProductId, MYSQL_ASSOC)) {
$purchasedProductIdarray[] = $row['products_id'];
}
while ($row = mysql_fetch_array($DSID, MYSQL_ASSOC)) {
$DSIDarray[] = $row['id'];
}
//compare $purchasedProductIdarray[] to $DSIDarray[], then save the comparason in the same order as $purchasedProductIdarray[]
任何幫助,將不勝感激。 另外,如果您知道實現此目標的更好方法,請告訴我!
您可以使用array_intersect
比較2個數組
$new_array = array_intersect($purchasedProductIdarray, $DSIDarray);
這將保存在平等IDS $new_array
以相同的順序在$purchasedProductIdarray
。
當您說“保存比較”時,您的意思是獲取同時存在於其中的元素的列表,對嗎? 你會這樣
$intersect = array_intersect($purchasedProductIdarray, $DSIDarray);
但是,在這種情況下,您根本不應該那樣做。 您可以首先從數據庫中獲取它。 聽起來orders_products.products_id
與drop_shippers.id
相匹配。 如果是這種情況,並且您的電子郵件列在drop_shippers
表中,則查詢將類似於以下內容:
SELECT products_id, email FROM orders_products
INNER JOIN drop_shippers ON drop_shippers.id=orders_products.products_id
WHERE orders_products.orders_id='{$orderid[0]}'
此外,不建議使用mysql擴展名; 您應該使用mysqli或PDO。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.