0

I have an action to check. If it has been set false in $Actions I need to do stuff.

$Actions['edit'] => true;
$Actions['delete'] => false;
$Actions['foo'] => true;
$Actions['bar'] => false;

$actionToCheck = 'delete';

Attempt 1:

$falseActions = '???';

if ( in_array( $actionToCheck, $falseActions ) ) {
    //do stuff
}

Attempt 2:

if ( in_array( $actionToCheck, $Actions, false ) ) {
    //do stuff
}
4
  • It is not clear what you are trying to do here. It seems to me you know the array and you know the key you want to evaluate within the array, so it should be as simple as if ($Actions[$actionToCheck]) { // do something if true } Commented Oct 1, 2014 at 17:53
  • I have an action to check. If it has been set false in ´$Actions´ I need to do stuff. Commented Oct 1, 2014 at 17:55
  • 1
    Ok. Then if (!$Actions[$actionToCheck]) { // do something if false} Commented Oct 1, 2014 at 17:56
  • If you are trying to find out if false is in the $Actions array, then in_array() can do that, but it won't return the key value that is false. Commented Oct 1, 2014 at 18:01

3 Answers 3

1

If I get what you are trying to do... How about :

foreach ($Actions AS $cSetting => $bValue) {
    if ($bValue === false) {
        print($cSetting . ' is false');
    }
}

this will allow you to do a targeted action against each setting that is false.

Sign up to request clarification or add additional context in comments.

Comments

0

I hope I understand your question correctly, it is a little vague.

The in_array() function checks to see if a value is in an array. What you appear to be looking at is if a key exists. You will need to use the array_key_exists() function. Then once you have confirmed it, you can move forward with the rest of your code.

$Actions['edit'] => true;
$Actions['delete'] => false;

$actionToCheck = 'delete';

if(array_key_exists($actionToCheck, $Actions)) {
    //do stuff with the $Actions['delete'] value (which is 'false')
}

See more in the PHP documentation.

2 Comments

This would return true when ´$actionToCheck = 'edit';´ but it should only if the checked action has been set flase in ´$Actions`.
This will return true when $actionToCheck is equal to the key value of $Actions that you are looking at, which appears to be what you are trying to do.
0

EDIT: I notice you have multiple false values in your example. So:

<?php
if ($false_keys = array_keys($Actions, false, true)) {
    foreach ($false_keys as $action) {
        //do stuff with $action
    }
}

Your question is quite unclear...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.