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

时间:2014-03-12 17:16:58

标签: php arrays multidimensional-array

我试图自学多维数组如何工作以及如何在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
    )

    )   

所以对我而言似乎它循环三次(匹配的项目数量)以及每次将匹配的项目放入数组中。

我想要的是在非匹配数组中与DON匹配的所有项目(使用Title作为键)和匹配数组中的DO。我想错过了一些非常明显的东西。

任何帮助都会很棒 关注迈克

1 个答案:

答案 0 :(得分:1)

我无法简单地复制/粘贴您的数组定义,因此我没有进行测试,但是您需要检查是否相等,如果找到则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;
    }
}