i'm working on a module where i get an array of content types like so:

$my_node_types = array('page' => 0, 'story' => 'story')

in my hook_nodeapi function i'm checking to see if the node is of the right content type by running this line:

if (!in_array($node->type, $my_node_types)) {
break;
}

no matter what i put in there though, it always returns true. is there something that i'm doing wrong? a majority of my code so far is straight out of the pro drupal development book so i'm not sure why it's not working for me.

Comments

deekayen’s picture

By prefixing in_array() with !, you're saying

If $node->type is not in $my_node_types, break out of this if statement and continue on with executing this function.

in_array() only examines the values of the array, so it only sees 0 and story in $my_node_types. For the if to be true, $node->type either has to have a value of 0 or story, so it can't always be true.

Either way, if statement with a break in it essentially does nothing, because even if the if condition is met, the break doesn't do anything - it only works on loops and switches.

BladeRider’s picture

Try using the PHP array_values() function like so:

  if (!in_array($node->type, array_values($my_node_types))) {
    ...
  }

This will test only against the values(!). There is a corresponding array_keys() function also.

BladeRider’s picture

double post :/

jm2099’s picture

Use boolean 'true' as third parameter in IN_ARRAY(needle, haystack, true) to force type check.

mattyoung’s picture