我正在尝试将一个数组与另一个数组进行比较。
$dropship_array = array();
$dropship_query = tep_db_query("select id, email from drop_shippers");
while ($dropship = tep_db_fetch_array($dropship_query)) {
$dropship_array[] = array('id' => $dropship['id'],
'email' => $dropship['email']);
}
现在,$ dropship_array []包含:
Array (
[0] => Array (
[id] => 0
[email] => none
)
[1] => Array (
[id] => 2
[email] => dropshipper1@gmail.com
)
[2] => Array (
[id] => 5
[email] => dropshipper2@gmail.com
)
[2] => Array (
[id] => 10
[email] => dropshipper3@gmail.com
)
)
现在,我需要将上面的数组(dropship_array ['id'])与下面的数组(products_array ['dsid'])进行比较。 products_array []数组已被拆分,以便根据每个[dsid]将每个单独的数组组合在一起。因此,每当在一组产品和drop ship id之间找到匹配时,就需要执行一个功能。
$products_array = array();
$products_query = tep_db_query("select products_id, products_name, drop_ship_id from " . TABLE_ORDERS_PRODUCTS . " where orders_id = '" . (int)$orders['orders_id'] . "' order by products_name");
while ($products = tep_db_fetch_array($products_query)) {
$products_array[] = array('id' => $products['products_id'],
'text' => $products['products_name'],
'dsid' => $products['drop_ship_id']);
}
Array (
[0] => Array (
[0] => Array (
[id] => 793
[text] => Gun Dog Training Book
[dsid] => 8
)
)
[1] => Array (
[0] => Array (
[id] => 789
[text] => Top Dog II Training DVD Video
[dsid] => 5
)
[1] => Array (
[id] => 237
[text] => Tri-Tronics Retriever Training Book
[dsid] => 5
)
)
)
这是否需要foreach功能?
答案 0 :(得分:1)
<?php
$one = array(
'0' => array(
'id' => '0',
'email' => 'none'
),
'1' => array(
'id' => '1',
'email' => 'none'
),
'2' => array(
'id' => '2',
'email' => 'none'
),
'3' => array(
'id' => '3',
'email' => 'none'
)
);
$two = array(
'0' => array(
'0' => array(
'id' => '793',
'text' => 'derp',
'dsid' => '8'
)
),
'1' => array(
'0' => array(
'id' => '793',
'text' => 'derp',
'dsid' => '8'
),
'1' => array(
'id' => '793',
'text' => 'derp',
'dsid' => '3'
)
),
);
foreach($one as $item) {
foreach($two as $compare) {
if(is_array($compare)) {
foreach($compare as $multicompare) {
if($multicompare['dsid'] == $item['id']) {
// Perform function
}
}
}
}
}
}
?>
打破它:
foreach()
遍历drop_shippers表中的第一个数组。foreach()
遍历您的下一个数组并检查该项是否为数组,如果是,则将dsid
与第一个数组中的id
进行比较。