【问题标题】:Getting error while trying to match two array using PHP尝试使用 PHP 匹配两个数组时出错
【发布时间】:2017-03-28 22:09:13
【问题描述】:

使用 PHP 比较两个数组时出现以下错误。

错误:

注意:未定义的偏移量: /opt/lampp/htdocs/test/search.php 第 7 行中的 1

注意:未定义的偏移量: 1 在 /opt/lampp/htdocs/test/search.php 第 15 行
删除值

注意:未定义的偏移量: /opt/lampp/htdocs/test/search.php 第 7 行中的 2

注意:未定义的偏移量: 2 in /opt/lampp/htdocs/test/search.php 第 15 行
删除值

我在下面解释我的代码。

$maindata=array(array('id'=>3),array('id'=>7),array('id'=>9));
 $childata=array(array('id'=>3),array('id'=>45));
for($i=0;$i<count($maindata);$i++){
      //print_r($childata);
       if(count($childata) > 0){
           if(in_array($childata[$i],$maindata)){
               echo "get the value \n".$maindata[$i]['id'];
               echo "insert the value \n".$maindata[$i]['id'];
               unset($childata[$i]);
               if(count($childata) > 0){
                   $childata=array_values($childata);
               }
           }else{
               echo "delete the value \n".$childata[$i]['id'];
               unset($childata[$i]);
               if(count($childata) > 0){
                   $childata=array_values($childata);
               }
           }
       }else{
           echo "get the value \n".$maindata[$i]['id'];
           echo "insert the value \n".$maindata[$i]['id'];
       }
    }

请帮我解决这个错误。

【问题讨论】:

  • 你想从条件中检查什么......
  • @PHPGeek :在这里我需要检查$childata 值是否存在于$maindata 数组中,然后两个echo 将执行并且该特定值将被删除。一旦$childata 长度将是零,然后第二个 else 部分将执行。
  • 那我觉得你需要一个一维数组而不是二维数组..这个数组有没有需要...
  • @PHPGeek : 你能把这个改正吗?
  • 为什么不使用array_udiff_assoc 进行回调之类的?

标签: php arrays


【解决方案1】:

有点不清楚为什么您在循环中的 ifelse 子句中做完全相同的事情。然而;下面是一个片段,可能会让您对如何重新审视您的方法有所了解。 Quick-Test Here:

<?php

    $mainData   = array( array('id'=>3), array('id'=>7), array('id'=>9) );
    $childData  = array( array('id'=>3), array('id'=>45) );

    foreach($mainData as $iKey=>$subArray){
        echo "get the value \n"     . $mainData[$iKey]['id']  . "<br />";
        echo "insert the value \n"  . $mainData[$iKey]['id']  . "<br />";
        // CHECK TO SEE THAT THE ARRAY $childData IS NOT EMPTY
        if(!empty($childData)){
            // THEN LOOP THROUGH THE $childData ARRAY
            // AND IF YOU SIMILAR ELEMENT IS FOUND TO  EXIST IN $mainData
            // DELETE (UNSET) ITS EQUIVALENT IN THE $childData
            foreach($childData as $iKey2=>$subArray2){
                if(in_array($subArray2, $mainData)){
                    unset($childData[$iKey2]);
                }else{
                    // YOU ARE DOING EXACTLY THE SAME THING YOU DID IN THE IF CLAUSE
                    // HERE AGAIN IN THE ELSE CLAUSE: IS THAT WHAT YOU REALLY WANT?
                    // THAT IS YOU ARE REPEATING: "unset($childData[$iKey2]);" ???
                    echo "delete the value \n"  . $childData[$iKey2]['id'] . "<br />";
                    unset($childData[$iKey2]);

                    // MOST LIKELY; YOU DON'T NEED THIS ELSE CLAUSE AT ALL
                    // unset($childData[$iKey2]);
                }
            }
            // GET THE ARRAY VALUES IF THE $childData is NOT EMPTY
            $childData  = (!empty($childData))? array_values($childData):$childData;
        }
    }

【讨论】:

  • @Pioz :让我明确一下我的要求。对于两个数组之间的任何公共值,两个 echo 语句将执行,而对于剩余值,单个 echo 语句将执行。
  • @subhra 再次检查代码......eval.in/677729 顺便说一下,名字是 Poiz 而不是 Pioz ;-)
  • 先抱歉。好的,它没有按预期工作。让我再次解释一下。这里对于来自第一个数组 ($mainData) 的所有值,两个 echo 必须执行。如果第二个数组 (here 3) 中有任何公共值,则将被忽略。除此之外,任何剩余的值都在第二个数组('i,e-$childData')中,因为这个单一的echo 将执行。
  • 这里的输出应该像从头开始(get the value 3 insert the value 3 delete the value 45 get the value 7 insert the value 7 get the value 9 insert the value 9)。假设$childData = array( array('id'=&gt;34), array('id'=&gt;45) );在这种情况下它不起作用。
  • @subhra LOL :-) 更新后...再试一次...eval.in/677744
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多