【问题标题】:Compare multidimensional arrays for different entries and storing them比较不同条目的多维数组并存储它们
【发布时间】:2014-03-12 17:16:57
【问题描述】:

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

    )   

所以在我看来,它好像循环了三遍(匹配的项目数量),每次都将匹配的项目放入数组中。

我想要的是非匹配数组中不匹配的所有项目(使用标题作为键)以及匹配数组中匹配的项目。我想我遗漏了一些非常明显的东西。

任何帮助都会很重要 考虑迈克

【问题讨论】:

    标签: php arrays multidimensional-array


    【解决方案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;
        }
    } 
    

    【讨论】:

    • 非常感谢您的快速响应,它们都可以工作,并且我在一定程度上理解代码,但我只是想知道是否有理由让我像第一个一样保留密钥一个或简单地创建一个新索引,如第二个示例中所示
    • @Mike:在第一个示例中,我使用$key,以便稍后在if 中引用它并检查它。我无法使用[],因为它可能是 0 或 100 等等……我以后无法检查它,因为我不知道它是什么。在第二个示例中,这并不重要,因为我设置了 $match = truefalse,我可以检查一下。
    • @Mike:另外,在第二个示例中,如果您删除 break,那么它允许多个匹配项(任一数组中的多个匹配 Title
    • 感谢您的帮助!我一直在摆弄代码以更好地理解它。非常感谢您的回复。
    猜你喜欢
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 2017-07-17
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多