在 php 中编码的更简单方法“if ( $x == $a[1] || $x == $a[2] || $x == $a[3] ....)

发布于 2024-11-03 13:37:00 字数 314 浏览 2 评论 0原文

正如标题所述,有什么更简单的方法来做

if ( $somevariable == $somearray[1] || $somevariable  == $somearray[3] || $somevariable  == $somearray[10] ) 

似乎即使有 3 个变量..也会有一个快捷方式。

我知道这不起作用,但类似的东西会很好:

if ($somevariable == $somearray[1],$somearray[3],$somearray[10]) {

As the title states, whats a more simple way to do

if ( $somevariable == $somearray[1] || $somevariable  == $somearray[3] || $somevariable  == $somearray[10] ) 

Seems like even with 3 variables.. there would be a shortcut.

I know this doesnt work, but something like would be nice:

if ($somevariable == $somearray[1],$somearray[3],$somearray[10]) {

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

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

发布评论

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

评论(4

染墨丶若流云 2024-11-10 13:37:00

一种选择

if ( in_array( $somevariable, array( $somearray[1], $somearray[3], $somearray[10] ) ) {}

One option

if ( in_array( $somevariable, array( $somearray[1], $somearray[3], $somearray[10] ) ) {}
夜夜流光相皎洁 2024-11-10 13:37:00

您可以使用 in_array

You could use in_array.

硬不硬你别怂 2024-11-10 13:37:00

其实你的代码已经没问题了。
如果您不想深入重构,请保持原样。因为:

  • 它完全符合其预期目的 - 根据某些条件检查某些变量。
  • 更重要的是:它看起来像是根据某些条件检查某些变量的代码。所以,你总能知道它是做什么的。这比一些花哨的短地更重要。

在追求简洁的同时,不要忘记可读性
唯一可接受的解决方案是可读性不低于当前解决方案的解决方案。 读起来(字面意思:如果你大声朗读)就像它的意图一样。
示例:通过创建这样的函数,

function whateverCondition($variable,$array) {
  return ( $variable == $array[1] 
        || $variable  == $array[3] 
        || $variable  == $array[10]); 
}

您可以稍后以这种方式调用它,

if (whateverCondition($somevariable,$somearray))

同时保持简洁性和可读性(如果您命名正确)。

正如评论中提到的,参数似乎有点多余。这取决于。如果它是一个函数,请将它们保留在适当的位置。
如果此函数将成为某个适当的的方法 - 您可以省略它们,使其成为

if ($user->meetCondition())

In fact, your code is already all right.
If you don't want to dive into depths of refactoring, just leave it as is. Because:

  • it does exactly what it's intended for - checks some variable against some conditions.
  • more important: it looks like the code to check some variable against some conditions. So, you could always tell what does it do. That's way more important than some fancy shortland.

In pursue for conciseness, you should not forget readability.
The only solution acceptable is one which no less readable than your current one. Which reads (literally: if you read it aloud) like it's intentded.
Example: by making such a function,

function whateverCondition($variable,$array) {
  return ( $variable == $array[1] 
        || $variable  == $array[3] 
        || $variable  == $array[10]); 
}

you can call it later this way,

if (whateverCondition($somevariable,$somearray))

keeping both conciseness and readability (if you name it properly).

As it was mentioned in comments, parameters seems a bit redundant. it depends. if it's going to be a function, keep them in place.
if this function going to be a method of some appropriate class - you can omit them, making it

if ($user->meetCondition())
淡水深流 2024-11-10 13:37:00

另一种选择使用 array_intersect_key()

if (in_array($var, array_intersect_key($somearray, array_flip(array(1, 3, 10)))))
{
}

如果您有大量索引需要检查,这非常有用。


在可变参数函数中,正如 @knittl 建议的那样:

function in_array_keys($needle, $haystack)
{
    $keys = array_flip(array_slice(func_get_args(), 2);
    return in_array($needle, array_intersect_key($haystack, $keys)));
}

var_dump(in_array_keys($var, $somearray, 1, 2, 3));

Another option using array_intersect_key():

if (in_array($var, array_intersect_key($somearray, array_flip(array(1, 3, 10)))))
{
}

This is mostly useful if you have a large numbers of indexes to check.


In a variadic function, as @knittl suggested:

function in_array_keys($needle, $haystack)
{
    $keys = array_flip(array_slice(func_get_args(), 2);
    return in_array($needle, array_intersect_key($haystack, $keys)));
}

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