¿Cómo verificar un valor único?

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

Por ejemplo:

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!

preguntado el 08 de noviembre de 11 a las 18:11

De una sola mano: if (in_array("south", $array) && !in_array(allothervalues, $array)) { // South is the only value } -

No I don't really understand... how is your question related to the switch? -

I'd like to help but the example makes 0 sense. -

4 Respuestas

Yo lo haría:

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'



?>

respondido 08 nov., 11:23

Thanks, but the array can have 0, 1, 2, 3 or 4 elements;-) - Jordy

Hmm you said: How can I make a script that checks for example if the only value in the array is 'south'? Did your requirements change? - Ben

Updated with what I think is a more helpful answer - Ben

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

respondido 08 nov., 11:22

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';
}

respondido 08 nov., 11:22

+1 usando array_diff. That's the tool of choice (or array_intersect and the like) I would say. - hakre

I think an easier solution would be:

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

respondido 08 nov., 11:22

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.