如何检查唯一的值?

发布于 2024-12-14 03:22:39 字数 443 浏览 1 评论 0原文

我有一个带有随机变量名的开关和一个可以包含左、右、上、下值的数组。

例如:

switch ($i) {
    case 0:
        $name='something1';
        $array=array('north', 'south', 'west');
        break;
    case 1:
        $name='something2';
        $array=array('north', 'south');
    case 2:
        $name='something3';
        $array=array('south');
}

如何制作一个脚本来检查数组中的唯一值是否为“南”?在我的脚本中,输出将是 some3,如果我检查值的北和南,脚本将输出 some2?

希望你能理解我。谢谢!

I have an switch with a random variable name and a array which can contain the values left, right, up and down.

For example:

switch ($i) {
    case 0:
        $name='something1';
        $array=array('north', 'south', 'west');
        break;
    case 1:
        $name='something2';
        $array=array('north', 'south');
    case 2:
        $name='something3';
        $array=array('south');
}

How can I make a script that checks for example if the only value in the array is 'south'? In my script the output will be something3, and if I check for the value's north and south, the script would output something2?

Hope you understand me. Thanks!

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

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

发布评论

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

评论(4

红墙和绿瓦 2024-12-21 03:22:39

我会这样做:

if((count($array) == 1) && ($array[0] == 'south')){
//code here
}

这仅在数组只有一个元素时才有效。

好吧,我认为这是实现这一目标的一种非常简单的方法:

<?php

function checktangent($array,$tocheck){

$tocheck = explode(',', str_replace(' ', '', $tocheck));

if(count($tocheck) == count($array)){

    $foundall = true;

    foreach($tocheck as $value){
        if(!in_array($value, $array))
          $foundall = false;
        }
    return $foundall;
    }
else
    return false;

}


//Use like:

$array = array('north', 'south', 'west');


if(checktangent($array, 'north, south'))
    echo 'found';
    else
        echo 'not found'



?>

I would do:

if((count($array) == 1) && ($array[0] == 'south')){
//code here
}

This will only work if the array has one element.

Ok, I think this is a pretty foolproof way of accomplishing this:

<?php

function checktangent($array,$tocheck){

$tocheck = explode(',', str_replace(' ', '', $tocheck));

if(count($tocheck) == count($array)){

    $foundall = true;

    foreach($tocheck as $value){
        if(!in_array($value, $array))
          $foundall = false;
        }
    return $foundall;
    }
else
    return false;

}


//Use like:

$array = array('north', 'south', 'west');


if(checktangent($array, 'north, south'))
    echo 'found';
    else
        echo 'not found'



?>
回忆躺在深渊里 2024-12-21 03:22:39

您可以直接在 PHP 中比较数组。但要小心,因为这也会比较值的顺序。

var_dump(array(1, 2) == array(1, 2)); //true
var_dump(array(1, 2) == array(2, 1)); //false

如果你能保证相同的顺序,你可以这样做:

<?php
    $directions = array('north', 'south');
    switch($directions) {
        case array('north'):
        echo 'north';
        break;

        case array('south'):
        echo 'south';
        break;

        case array('north', 'south'):
        echo 'north south';
        break;
    }
?>

http://codepad.viper-7。 com/TCoiDw

You can compare arrays directly in PHP. Be careful though, because this also compares the order of the values.

var_dump(array(1, 2) == array(1, 2)); //true
var_dump(array(1, 2) == array(2, 1)); //false

If you can guarantee the same order, you could do something like this:

<?php
    $directions = array('north', 'south');
    switch($directions) {
        case array('north'):
        echo 'north';
        break;

        case array('south'):
        echo 'south';
        break;

        case array('north', 'south'):
        echo 'north south';
        break;
    }
?>

http://codepad.viper-7.com/TCoiDw

霞映澄塘 2024-12-21 03:22:39

如果我正确理解你在寻找什么,这应该可行

if (false == count(array_diff(array('north', 'south', 'west'), $array))) {
   echo 'something1';
} else if (false == count(array_diff(array('north', 'south'), $array))) {
   echo 'something2';
} else if (count($array) == 1 AND current($array) = 'south') {
   echo 'something3';
}

This should work if I understand what your looking for correctly

if (false == count(array_diff(array('north', 'south', 'west'), $array))) {
   echo 'something1';
} else if (false == count(array_diff(array('north', 'south'), $array))) {
   echo 'something2';
} else if (count($array) == 1 AND current($array) = 'south') {
   echo 'something3';
}
维持三分热 2024-12-21 03:22:39

我认为更简单的解决方案是:

 array_unique($array);
 if (count($array) == 1 && in_array('south', $array))
     // Only south exists.

I think an easier solution would be:

 array_unique($array);
 if (count($array) == 1 && in_array('south', $array))
     // Only south exists.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文