1

I'm doing some content importing using the node import module in drupal. My problem is that I'm getting errors on data that looks like it should be working smoothly. This is the code at issue:

if (count($allowed_values) && !array_key_exists($item['value'], $allowed_values)) { //$allowed_values[$item['value']] == NULL) {
  print "||||" . $item['value'] . "||||";
  print_r($allowed_values);

And this is a sample of what is printing:

||||1||||Array ( [0] => no [1] => Zicam® Cold Remedy Nasal Gel Spray Single Hole Actuator (“Jet”) ) ||||1||||Array ( [0] => No [1] => Yes )

It looks to me like it's saying that "1" is not in the array, when printing the way "1" is clearly visible. If I replace the existing module code with the commented out check, no error is thrown.

5
  • what is in item and allowed_values? Commented May 27, 2011 at 21:56
  • I included what was printing. Commented May 27, 2011 at 22:05
  • Is item an actual array? The docs for array_key_exists say it no longer works properly for object for PHP 5.3. What was a var_dump() of $item['value'] and $allowed_values look like? print_r is nice for content, but var_dump also reports type/size information. Commented May 27, 2011 at 22:06
  • Ah... var_dump($item['value']); // reports bool(true). So I guess that is my problem then? Commented May 27, 2011 at 22:22
  • I knew it'd be some dumb PHP problem. :P I hate this language! Commented May 27, 2011 at 22:38

1 Answer 1

2

Your code is not complete and i cannot reproduce the error.

Allow me to adjust your example:

<?
$item = array('value' => 1);
$allowed_values = array(0 => 'no',1 => 'yes');

echo "needle:";
var_dump($item['value']);
echo "haystack:";
var_dump($allowed_values);

if (count($allowed_values) && !array_key_exists($item['value'], $allowed_values)) {
        echo "needle hast not been found or haystack is empty\n";
} else {
        echo "needle has been found\n";
}

gives the desired output:

needle:int(1)
haystack:array(2) {
  [0]=>
  string(2) "no"
  [1]=>
  string(3) "yes"
}
needle has been found

PHP also works when you assign the needle a string and not an integer. It is some sort of lossy type conversion that can be really convenient but also a pain in the ass. Often you dont know whats going on and errors are caused.

But still. I bet you have something wrong with your variable types.

You should dump them and see what is really in there.

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

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.