By jjkiesch on
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
! means "not"
By prefixing in_array() with !, you're saying
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.
Be careful what you are testing...
Try using the PHP
array_values()function like so:This will test only against the values(!). There is a corresponding
array_keys()function also.double post :/
double post :/
Use boolean 'true' as third
Use boolean 'true' as third parameter in IN_ARRAY(needle, haystack, true) to force type check.
see
see http://us.php.net/in_array#86695