views:

58

answers:

3

There is an array of user states stored in the session. This works:

<?php if ($_SESSION['_app_user']['data']['state']['1']) { ?>

  <p>User has state 1</p>

<?php } ?>

But, selecting multiple states doesn't:

<?php if ($_SESSION['_app_user']['data']['state']['1,6,10']) { ?>

  <p>User has state 1 or 6 or 10</p>

<?php } ?>

How can you check on multiple states?

+4  A: 

By checking multiple.

You may find it easier to store the least common denominator to a temporary variable:

$s = $_SESSION['_app_user']['data']['state'];
if(isset($s[1]) || isset($s[6]) || isset($s[10])) {
    echo 'Tahdah!';
}
unset($s);

Also, please use quotes for your strings. It makes code clearer, and saves the PHP interpreter a bit of effort guessing that you mean a string instead of, say, a constant named _app_user :)

Matchu
Thanks! Worked great!
m1755
What do you think about the comment from Stefan K about using `array_key_exists`?
m1755
I'm sure you've already arrived at this, but yeah. `isset` and `array_key_exists` have slightly different behavior, but `isset` has the better performance, if its behavior is acceptable.
Matchu
+1  A: 

May be it's better to use "array_key_exists" function to check if the given index exists in the array. See Example #2 array_key_exists() vs isset(). http://bg2.php.net/manual/en/function.array-key-exists.php

Stefan K
How would you implement `array_key_exists` with the example I posted? Or are you saying I should use Matchu's solution with `array_key_exists` instead of `isset`?
m1755
if(array_key_exists(1, $s) || ...)
Stefan K
Yes, Matchu's solution with array_key_exists instead of isset.
Stefan K
Thanks Stefan! I am curious why it would be better?
m1755
If you don't play with null value inside the array(), isset will be much faster as it's not a function. :) I personally never use array_key_exists()... for performance reasons.
Savageman
It's explained in the Example #2 from the posted link.isset() does not return TRUE for array keys that correspond to a NULL value, while array_key_exists() does. <?php$search_array = array('first' => null, 'second' => 4);// returns falseisset($search_array['first']);// returns truearray_key_exists('first', $search_array);?>
Stefan K
Thanks for the clarification. I don't believe I am using any null values so I will stick with `isset`.
m1755
+1  A: 

You could also use array_intersect to check an array of states against your user states. For example:

$user_states = $_SESSION['_app_user']['data']['state'];
$check_states = array( 1, 6, 10 );

$matches = array_intersect(array_keys($user_states), $check_states);
if(!empty($matches))
{
    echo "User has valid states: \n";
    foreach($matches as $_state)
    {
        echo " - {$_state}\n";
    }
}
else
{
    echo "Sorry. Not found.";
}

The function checks whether any two elements in the arrays match, and returns all the matches. Meaning that in that code, the $matches array would be a list of all the states that the user has and are in your list.

Atli
Thanks! I couldn't get this working properly but I like the approach.
m1755
No problem. Let me know if I can help get it sorted out.
Atli