万一如何使用数组?

发布于 2024-12-15 10:00:59 字数 412 浏览 5 评论 0原文

在有开关的情况下如何使用数组?这不起作用,并且始终采用默认值 (3):

    switch ($my_array) {

        case array('george','paul'):
            $id     = 1;
            break;
        case array('paul','max'):
            $id     = 2;
            break;
        case array('eric'):
            $id     = 3;
            break;

        //default
        default:
            $id     = 3;
            break;

    }

How can I use an array in the case of a switch? This doesn't work and always take the default (3):

    switch ($my_array) {

        case array('george','paul'):
            $id     = 1;
            break;
        case array('paul','max'):
            $id     = 2;
            break;
        case array('eric'):
            $id     = 3;
            break;

        //default
        default:
            $id     = 3;
            break;

    }

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

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

发布评论

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

评论(4

梦里人 2024-12-22 10:00:59

根据有关数组运算符的 PHP 手册,您的示例应该可以工作:

$a == $b:如果 $a 和 $b 具有相同的键/值对,则为 TRUE。

由于 switch/case 使用弱比较,因此使用 == 运算符来比较数组。

我在键盘上放置了一个工作示例: http://codepad.org/MhkGpPRp

Your example should work, according to the PHP manual on array operators:

$a == $b: TRUE if $a and $b have the same key/value pairs.

Since switch/case uses weak comparison, arrays are compared by using the == operator.

I've put a working example onto codepad: http://codepad.org/MhkGpPRp

时光清浅 2024-12-22 10:00:59

PHP 可以打开数组,但您确实需要所有元素具有完全相同的键才能成功进行比较。您可能需要使用 array_values() 来规范化键$我的_数组。否则它应该有效。 $my_array = array('paul','max'); 应该给出 $id=2。

PHP can switch on arrays, though you do need to have exactly the same keys of all the elements for the comparison to succeed. You might need to use array_values() to normalize the keys of $my_array. Otherwise it should work. $my_array = array('paul','max'); should give $id=2.

梦行七里 2024-12-22 10:00:59

你可以尝试使用这样的:

switch (serialize($junctions)) {

    case serialize(array('george','paul')):
        $id     = 1;
        break;
    case serialize(array('paul','max')):
        $id     = 2;
        break;
    case serialize(array('eric')):
        $id     = 3;
        break;

    //default
    default:
        $id     = 3;
        break;

}

但你真的想要它吗?

You can try to use some like this:

switch (serialize($junctions)) {

    case serialize(array('george','paul')):
        $id     = 1;
        break;
    case serialize(array('paul','max')):
        $id     = 2;
        break;
    case serialize(array('eric')):
        $id     = 3;
        break;

    //default
    default:
        $id     = 3;
        break;

}

But you really want it?

耳根太软 2024-12-22 10:00:59

switch() 语句旨在匹配单个条件。我不认为有办法使用开关来实现这一点。您需要使用 if else 链:

if (in_array('george', $array) && in_array('paul', $array) && !in_array('max', $array)) {
  $id = 1;
}
else if(in_array('paul', $array) && in_array('max', $array)) {
 $id = 2;
}
else if (in_array('eric', $array)) {
  $id = 3;
}
else {
  $id = 3;
}

根据 数组运算符规则,可以使用==,但数组成员的顺序必须相同。从技术上讲,只有键和值必须匹配,但对于数字索引数组,这相当于成员具有相同的数字顺序。

if ($array == array('john', 'paul')) {
  $id = 1;
}
else if ($array == array('paul', 'max')) {
  $id = 2;    
}
else if ($array == array('eric')) {
  $id = 3;
}
else {
  $id = 3;
}

switch() statements are intended to match single conditions. I don't think there will be a way to use a switch for this. You need to use an if else chain instead:

if (in_array('george', $array) && in_array('paul', $array) && !in_array('max', $array)) {
  $id = 1;
}
else if(in_array('paul', $array) && in_array('max', $array)) {
 $id = 2;
}
else if (in_array('eric', $array)) {
  $id = 3;
}
else {
  $id = 3;
}

According to array operator rules, you can use ==, but the array members must be in the same order. Technically, it is only the keys and values that must match, but for a numeric-indexed array, this equates to the members being in the same numeric order.

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