如何使用 foreach 比较两个数组的数据?
我有这两个提要(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第一个嵌套循环将回显第一个提要中不存在于第二个提要中的项目 ID,第二个 foreach 将回显第二个提要中不存在于第一个提要中的 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
您可以将一个对象数组的 ID 存储为一个简单的一维数组,然后在循环另一个 feed 时,只需检查另一个 feed 中是否存在该 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.
Hope that helps.
如果我正确理解这个问题,这样的事情应该有效。正如其他问题中提到的那样,您可能最好在数组中执行此操作,但这也应该有效。
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.