比较不同条目的多维数组并存储它们



我试图教自己多维数组是如何工作的,以及如何在php中比较和操作它们,我已经创建了两个具有相同方案的数组,但每个数组中有一个不同的值。

第一个数组

      $datastuff1 = array( array(  Ttitle => "rose", 
                            Price => 1.25,
                            Number => 15 
                            ),
                     array( Ttitle => "daisy", 
                            Price => 0.75,
                            Number => 25
                           ),
                     array( Ttitle => "lilly", 
                            Price => 1.75,
                            Number => 3
                           ),
                     array( Ttitle => "orchid", 
                            Price => 1.15,
                            Number => 7 
                        )
                     );

第二个数组

     $datastuff2 = array( array(  Title => "rose", 
                            Price => 1.25,
                            Number => 15 
                            ),
                     array( Title => "daisy", 
                            Price => 0.75,
                            Number => 25
                           ),
                     array( Title => "nettle", 
                            Price => 2.75,
                            Number => 33
                           ),
                     array( Title => "orchid", 
                            Price => 1.15,
                            Number => 7 
                        )
                     );

我现在想循环遍历两个数组和foreach项,匹配(使用标题作为键)在两个数组添加到一个新的匹配数组和每个项目,不匹配在两个数组添加到我的不匹配数组

我的代码

    $matchingarray = array();
    $notmatchingarray = array();
    foreach($datastuff1 as $data1){
        foreach($datastuff2 as $data2){
           if($data2['Title']== $data1['Ttitle'])
           {
              $matchingarray[] = $data1;
           }
        else {
        $notmatchingarray[] = $data1;
            }
        }
    } 

但是当我使用

输出数组的内容时
   echo "<pre>";
   print_r($notmatchingarray);
   echo "</pre>"; 
我得到了输出
    Array
    (
     [0] => Array
     (
        [Ttitle] => rose
        [Price] => 1.25
        [Number] => 15
    )
   [1] => Array
    (
        [Ttitle] => rose
        [Price] => 1.25
        [Number] => 15
    )
   [2] => Array
    (
        [Ttitle] => rose
        [Price] => 1.25
        [Number] => 15
    )
   [3] => Array
    (
        [Ttitle] => daisy
        [Price] => 0.75
        [Number] => 25
    )
  [4] => Array
    (
        [Ttitle] => daisy
        [Price] => 0.75
        [Number] => 25
    )
   [5] => Array
    (
        [Ttitle] => daisy
        [Price] => 0.75
        [Number] => 25
    )
  [6] => Array
    (
        [Ttitle] => lilly
        [Price] => 1.75
        [Number] => 3
    )
  [7] => Array
    (
        [Ttitle] => lilly
        [Price] => 1.75
        [Number] => 3
    )
  [8] => Array
    (
        [Ttitle] => lilly
        [Price] => 1.75
        [Number] => 3
    )
  [9] => Array
    (
        [Ttitle] => lilly
        [Price] => 1.75
        [Number] => 3
    )
  [10] => Array
    (
        [Ttitle] => orchid
        [Price] => 1.15
        [Number] => 7
    )
  [11] => Array
    (
        [Ttitle] => orchid
        [Price] => 1.15
        [Number] => 7
    )
  [12] => Array
    (
        [Ttitle] => orchid
        [Price] => 1.15
        [Number] => 7
    )
    )   

所以对我来说,它似乎循环了三次(匹配的项的数量),每次都将匹配的项放入数组中。

我想要的是所有不匹配的项目(使用标题作为关键字)在非匹配数组和那些在匹配数组做。我想我漏掉了一些显而易见的东西。

任何帮助都是伟大的把迈克

我不能简单地复制/粘贴你的数组定义,所以我没有测试,但你需要检查相等,如果发现然后break内循环。此外,在内环之后,检查它是否添加到$matchingarray中,如果没有添加到$notmatchingarray中:

foreach($datastuff1 as $key => $data1){
    foreach($datastuff2 as $data2){
        //match add to $matchingarray
        if($data2['Title'] == $data1['Ttitle']) {
            $matchingarray[$key] = $data1; //use $key so we can check later
            break; //we have a match so why keep looping?
        }
    }
    //if no match add to $notmatchingarray
    if(!isset($matchingarray[$key])) { //we used $key so we can check for it
        $notmatchingarray[$key] = $data1; //don't need $key here but oh well
    }
}

另一种可能更容易遵循的方法:

foreach($datastuff1 as $key => $data1){
    $match = false; //no match
    foreach($datastuff2 as $data2) {
        //match add to $matchingarray
        if($data2['Title'] == $data1['Ttitle']) {
            $matchingarray[] = $data1;
            $match = true; //match
            break; //we have a match so why keep looping?
        }
    }
    //if no match add to $notmatchingarray
    if(!$match) {
        $notmatchingarray[] = $data1;
    }
} 

相关内容

  • 没有找到相关文章

最新更新