dronthpi05943
2014-06-15 14:56 阅读 49
已采纳

比较2个数组并使用PHP检索差异

I'm not a PHP expert but i'm trying Compare 2 arrays ( database relations and schema) in PHP, I need retrieve the differences between those 2 array, I'm using this php code

    <?php

function maxEntities($rela, $db){
    $maxenty = array();
    $found   = false;

     foreach ($rela as $valor1) {  
      print $valor1[0] . " |  ";
    }

    print " <br \>";
     foreach ($db as $valor2) {

      print  $valor2[0] . " |  ";
    }
 $maxenty = array_diff($rela[0], $db[0]);

    print " <br \> <br \>";

    foreach ($maxenty as $valor) {

      print "  " . $valor;
    }
}

and this code give me this output

 Sale | Customer | Sale_Fee | Sale | Location | Sale | Sale | Sale_Iten | Product | Customer | Location | Sale_Iten | Sale_Fee | Region | 
Customer | Customer_Type | Fee_Type | Location | Location_Type | Period | Product | Product_Type | Region | Sale | Sale_Fee | Sale_Iten | State | 

Sale Customer_Cust_Id 

and the output i expect is

Period, Customer_Type, State, Location Type, Product Type and Fee Type

how i can solve my problem ?

i also tried with foreach, but give me also a wrong output

 foreach ($rela as $relaV) {
        foreach ($db as $dbV) {

            if ($dbV[0] == $relaV[0]) {
                $found = true;
            }
            if (!$found) {
                $found   = false;
               $maxenty[] = $dbV[0];  
            } 
        }
    }

in this case my output was

Customer
Customer_Type
Fee_Type
Location
Location_Type
Period
Product
Product_Type
Region

and Customer, Region, Location are in both arrays

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    doutongwei4380 doutongwei4380 2014-06-15 15:23

    It is not very clear, how your arrays are structured. From your code so far I guess the following structures:

    $rela = array(array('Sale'), array('Customer'), array('Sale_Fee'), array('Sale'), array('Location'), array('Sale'), array('Sale'), array('Sale_Iten'), array('Product'), array('Customer'), array('Location'), array('Sale_Iten'), array('Sale_Fee'), array('Region'));
    $db = array(array('Customer'), array('Customer_Type'), array('Fee_Type'), array('Location'), array('Location_Type'), array('Period'), array('Product'), array('Product_Type'), array('Region'), array('Sale'), array('Sale_Fee'), array('Sale_Iten'), array('State'));
    

    If this is the structure of your arrays, then you can use the following code to get the difference (adopted from your foreach-approach, there may be other ways):

    $maxenty = array();
    foreach ($db as $dbValue) {
        $found = false;
        foreach ($rela as $relaValue) {
            if ($relaValue[0] == $dbValue[0]) {
                $found = true;
                break;
            }
        }
        if (!$found) {
            $maxenty[] = $dbValue[0];
        }
    }
    
    print_r($maxenty);
    

    This will give you the $maxenty as follows:

    Array
    (
        [0] => Customer_Type
        [1] => Fee_Type
        [2] => Location_Type
        [3] => Period
        [4] => Product_Type
        [5] => State
    )
    
    点赞 评论 复制链接分享

相关推荐