如何使用 foreach 比较两个数组的数据?

发布于 2025-01-05 21:42:41 字数 330 浏览 1 评论 0原文

我有这两个提要(feed1,feed2),它们都提供了一些ID,我试图通过循环它们来找出我是否可以匹配ID,如果它们匹配则不显示该ID。

foreach($feed->data as $item){
echo $item->id;
}

foreach($feed2->data as $item){
echo $item->id;
}

这是我在 PHP 中的代码,用于显示两个 foreach 循环中的所有 ID,但我希望它们相互嵌套,因此如果 feed1 和 feed2 中的 ID 匹配,则不会回显。所以我认为它们可能是某个地方的 if 语句。谢谢

i have these two feeds(feed1,feed2), they both provide some ID'S, I'm trying to find out by looping through both of them if i can match ID'S, and if their is a match don't display that ID.

foreach($feed->data as $item){
echo $item->id;
}

foreach($feed2->data as $item){
echo $item->id;
}

this is my code in PHP for displaying all of the ID's from two foreach loops, but i wanted them to be nested into each other, so if ID's in feed1 and feed2 match don't echo. so i presume their might be an if statement their somewhere. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

π浅易 2025-01-12 21:42:41
$foundflag=false;
foreach($feed->data as $item){
     foreach($feed2->data as $item1){
        if($item->id == $item1->id){
           $foundflag = true;
           $array[]=$item1->id;
        }
    }
    if(!$foundflag){
        echo $item->id;
    }
}
foreach($feed2->data as $item1){
     if(!(in_array($item1->id,$array))){
        echo $item1->id;
     }
}

第一个嵌套循环将回显第一个提要中不存在于第二个提要中的项目 ID,第二个 foreach 将回显第二个提要中不存在于第一个提要中的 id

$foundflag=false;
foreach($feed->data as $item){
     foreach($feed2->data as $item1){
        if($item->id == $item1->id){
           $foundflag = true;
           $array[]=$item1->id;
        }
    }
    if(!$foundflag){
        echo $item->id;
    }
}
foreach($feed2->data as $item1){
     if(!(in_array($item1->id,$array))){
        echo $item1->id;
     }
}

First nested loop will echo item id from first feed which is not present in second and second foreach will echo id from second feed which is not present in first feed

莫相离 2025-01-12 21:42:41

您可以将一个对象数组的 ID 存储为一个简单的一维数组,然后在循环另一个 feed 时,只需检查另一个 feed 中是否存在该 ID。

# Create your variables to store the ID's of the two feeds
$ids_1 = array();
$ids_2 = array();

# Populate the arrays
foreach ($feed->data as $item1) {
    $ids_1[] = $item1->id;
}
foreach ($feed2->data as $item2) {
    $ids_2[] = $item2->id;
}

# Loop through the first feed, and exclude the items that are the same in the second
foreach($feed->data as $item){
    if (!in_array($item->id, $ids2)) {
        echo $item->id;
    }
}

# Loop through the second feed, and exclude the items that are the same in the first
foreach($feed2->data as $item){
    if (!in_array($item->id, $ids1)) {
        echo $item->id;
    }
}

希望有帮助。

You could store the ID's of the one array of objects as a simple 1d array, and then whilst looping through the other feed, just check if the ID exists in the other.

# Create your variables to store the ID's of the two feeds
$ids_1 = array();
$ids_2 = array();

# Populate the arrays
foreach ($feed->data as $item1) {
    $ids_1[] = $item1->id;
}
foreach ($feed2->data as $item2) {
    $ids_2[] = $item2->id;
}

# Loop through the first feed, and exclude the items that are the same in the second
foreach($feed->data as $item){
    if (!in_array($item->id, $ids2)) {
        echo $item->id;
    }
}

# Loop through the second feed, and exclude the items that are the same in the first
foreach($feed2->data as $item){
    if (!in_array($item->id, $ids1)) {
        echo $item->id;
    }
}

Hope that helps.

日久见人心 2025-01-12 21:42:41

如果我正确理解这个问题,这样的事情应该有效。正如其他问题中提到的那样,您可能最好在数组中执行此操作,但这也应该有效。

$items;
foreach($feed->data as $item){
  echo $item->id;
  foreach($feed2->data as $item2){
    echo $item2->id;
    $pos = strrpos($items, $item2->id);
    if ($item2->id != $item->id) && ($pos == false)
      $items = $item2->id + ", ";
    }
}

echo $items;

If I understand the question right something like this should work. You might be better to do it in an array as mentioned in other questions but this should work as well.

$items;
foreach($feed->data as $item){
  echo $item->id;
  foreach($feed2->data as $item2){
    echo $item2->id;
    $pos = strrpos($items, $item2->id);
    if ($item2->id != $item->id) && ($pos == false)
      $items = $item2->id + ", ";
    }
}

echo $items;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文