php while 循环,通过数组进行条件检查

发布于 2024-12-04 00:44:31 字数 153 浏览 0 评论 0原文

假设我有两个数组:

$a = array(1,2,3,4);
$b = array(3,4,5,6);

现在我必须在所有 $a 元素完全等于数组 $b 的情况下执行某些操作

I've two arrays suppose:

$a = array(1,2,3,4);
$b = array(3,4,5,6);

Now I've to do something while all $a element is exactly equals to array $b

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

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

发布评论

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

评论(3

原来是傀儡 2024-12-11 00:44:31

您尝试过 array_diff() 吗?

have you tried array_diff()?

迷爱 2024-12-11 00:44:31

在我看来,你可以做一些事情。

一个非常简单和基本的方法是循环并在其中有一些逻辑来检查 $a[$val] == $b[$val] 以及是否执行某些操作,否则不执行。

就像上面好心人所说的那样,PHP 中有一个名为 array_diff() 的函数,它可以计算数组中的差异。下面的示例取自 PHP.net 站点。

<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);
?>

Multiple occurrences in $array1 are all treated the same way. This will output :

Array
(
    [1] => blue
)

所以这完全取决于你想做什么。如果您想让您的陈述更清楚,请这样做,我将尝试相应地重新回答。

谢谢

There are a few things you could do here in my mind.

A VERY simple and basic way would be to loop through and have some logic in there to check if $a[$val] == $b[$val] and if it is do something, otherwise not.

Like the good people above said, there is a function in PHP called array_diff() which computes the difference in arrays. The below example is taken from the PHP.net site.

<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);
?>

Multiple occurrences in $array1 are all treated the same way. This will output :

Array
(
    [1] => blue
)

So it depends on exactly what you want to do. If you wish to make your statement more clear then please do so and I will try to re-answer accordingly.

Thanks

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