Martin
Martin

Reputation: 2017

PHP: How to check in_array() for group of false keys?

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
}

Upvotes: 0

Views: 77

Answers (3)

miken32
miken32

Reputation: 42711

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...

Upvotes: 0

DragonYen
DragonYen

Reputation: 968

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.

Upvotes: 1

Crackertastic
Crackertastic

Reputation: 4913

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.

Upvotes: 0

Related Questions