0

我正在尝试将一个数组与另一个数组进行比较。

$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 函数吗?

4

1 回答 1

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
                        }
                    }
                  } 
                }
            }
        }
?>

分解它:

  • Firstforeach()循环遍历 drop_shippers 表中的第一个数组。
  • Secondforeach()循环遍历下一个数组并检查该项目是否为数组,如果是,它将dsidid第一个数组中的 进行比较。
于 2013-11-07T02:09:33.607 回答